blob: cd41662feed427e693adcca9ace894e26e71e890 [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,
Lloyd Pique09594832018-01-22 17:48:03 -080082 int initialPowerMode)
Jesse Hallb7a05492014-08-14 15:45:06 -070083 : lastCompositionHadVisibleLayers(false),
84 mFlinger(flinger),
Dan Stoza9e56aa02015-11-02 13:00:03 -080085 mType(type),
86 mHwcDisplayId(hwcId),
Chih-Wei Huang27e25622013-01-07 17:33:56 +080087 mDisplayToken(displayToken),
Lloyd Pique09594832018-01-22 17:48:03 -080088 mNativeWindow(nativeWindow),
Jesse Hall99c7dbb2013-03-14 14:29:29 -070089 mDisplaySurface(displaySurface),
Lloyd Pique09594832018-01-22 17:48:03 -080090 mSurface{std::move(renderSurface)},
91 mDisplayWidth(displayWidth),
92 mDisplayHeight(displayHeight),
93 mPageFlipCount(0),
Jamie Gennisdd3cb842012-10-19 18:19:11 -070094 mIsSecure(isSecure),
Jesse Hall01e29052013-02-19 16:13:35 -080095 mLayerStack(NO_LAYER_STACK),
Prashant Malani2c9b11f2014-05-25 01:36:31 -070096 mOrientation(),
Lloyd Pique09594832018-01-22 17:48:03 -080097 mViewport(Rect::INVALID_RECT),
98 mFrame(Rect::INVALID_RECT),
99 mPowerMode(initialPowerMode),
100 mActiveConfig(0),
101 mActiveColorMode(ColorMode::NATIVE),
Yiwei Zhang7c64f172018-03-07 14:52:28 -0800102 mColorTransform(HAL_COLOR_TRANSFORM_IDENTITY),
Peiyong Lindd9b2ae2018-03-01 16:22:45 -0800103 mHasWideColorGamut(hasWideColorGamut),
Peiyong Lin62665892018-04-16 11:07:44 -0700104 mHasHdr10(false),
105 mHasHLG(false),
106 mHasDolbyVision(false)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800107{
Courtney Goeltzenleuchter5d943892017-03-22 13:46:46 -0600108 // clang-format on
Peiyong Lin62665892018-04-16 11:07:44 -0700109 for (Hdr hdrType : hdrCapabilities.getSupportedHdrTypes()) {
110 switch (hdrType) {
111 case Hdr::HDR10:
112 mHasHdr10 = true;
113 break;
114 case Hdr::HLG:
115 mHasHLG = true;
116 break;
117 case Hdr::DOLBY_VISION:
118 mHasDolbyVision = true;
119 break;
120 default:
121 ALOGE("UNKNOWN HDR capability: %d", static_cast<int32_t>(hdrType));
122 }
123 }
Jesse Hallffe1f192013-03-22 15:13:48 -0700124
Jesse Hallffe1f192013-03-22 15:13:48 -0700125 // initialize the display orientation transform.
126 setProjection(DisplayState::eOrientationDefault, mViewport, mFrame);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800127}
128
Lloyd Pique09594832018-01-22 17:48:03 -0800129DisplayDevice::~DisplayDevice() = default;
Mathias Agopian92a979a2012-08-02 18:32:23 -0700130
Jesse Hall02d86562013-03-25 14:43:23 -0700131void DisplayDevice::disconnect(HWComposer& hwc) {
132 if (mHwcDisplayId >= 0) {
133 hwc.disconnectDisplay(mHwcDisplayId);
Jesse Hall02d86562013-03-25 14:43:23 -0700134 mHwcDisplayId = -1;
135 }
136}
137
Mathias Agopian92a979a2012-08-02 18:32:23 -0700138bool DisplayDevice::isValid() const {
Peiyong Lin566a3b42018-01-09 18:22:43 -0800139 return mFlinger != nullptr;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800140}
141
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700142int DisplayDevice::getWidth() const {
Mathias Agopiana4912602012-07-12 14:25:33 -0700143 return mDisplayWidth;
144}
145
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700146int DisplayDevice::getHeight() const {
Mathias Agopiana4912602012-07-12 14:25:33 -0700147 return mDisplayHeight;
148}
149
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700150void DisplayDevice::setDisplayName(const String8& displayName) {
151 if (!displayName.isEmpty()) {
152 // never override the name with an empty name
153 mDisplayName = displayName;
154 }
155}
156
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700157uint32_t DisplayDevice::getPageFlipCount() const {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700158 return mPageFlipCount;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800159}
160
Chia-I Wub02087d2017-11-09 10:19:54 -0800161void DisplayDevice::flip() const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800162{
Mathias Agopian875d8e12013-06-07 15:35:48 -0700163 mFlinger->getRenderEngine().checkErrors();
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700164 mPageFlipCount++;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800165}
166
Dan Stoza71433162014-02-04 16:22:36 -0800167status_t DisplayDevice::beginFrame(bool mustRecompose) const {
168 return mDisplaySurface->beginFrame(mustRecompose);
Jesse Hall028dc8f2013-08-20 16:35:32 -0700169}
170
Dan Stoza9e56aa02015-11-02 13:00:03 -0800171status_t DisplayDevice::prepareFrame(HWComposer& hwc) {
172 status_t error = hwc.prepare(*this);
173 if (error != NO_ERROR) {
174 return error;
175 }
176
177 DisplaySurface::CompositionType compositionType;
178 bool hasClient = hwc.hasClientComposition(mHwcDisplayId);
179 bool hasDevice = hwc.hasDeviceComposition(mHwcDisplayId);
180 if (hasClient && hasDevice) {
181 compositionType = DisplaySurface::COMPOSITION_MIXED;
182 } else if (hasClient) {
183 compositionType = DisplaySurface::COMPOSITION_GLES;
184 } else if (hasDevice) {
185 compositionType = DisplaySurface::COMPOSITION_HWC;
186 } else {
187 // Nothing to do -- when turning the screen off we get a frame like
188 // this. Call it a HWC frame since we won't be doing any GLES work but
189 // will do a prepare/set cycle.
190 compositionType = DisplaySurface::COMPOSITION_HWC;
191 }
192 return mDisplaySurface->prepareFrame(compositionType);
193}
Jesse Hall38efe862013-04-06 23:12:29 -0700194
Mathias Agopianda27af92012-09-13 18:17:13 -0700195void DisplayDevice::swapBuffers(HWComposer& hwc) const {
Madhuri Athota88a905b2017-05-04 16:58:15 +0530196 if (hwc.hasClientComposition(mHwcDisplayId) || hwc.hasFlipClientTargetRequest(mHwcDisplayId)) {
Lloyd Pique144e1162017-12-20 16:44:52 -0800197 mSurface->swapBuffers();
Mathias Agopianda27af92012-09-13 18:17:13 -0700198 }
Mathias Agopian52e21482012-09-24 18:07:21 -0700199
Jesse Hall99c7dbb2013-03-14 14:29:29 -0700200 status_t result = mDisplaySurface->advanceFrame();
201 if (result != NO_ERROR) {
202 ALOGE("[%s] failed pushing new frame to HWC: %d",
203 mDisplayName.string(), result);
Mathias Agopian32341382012-09-25 19:16:28 -0700204 }
Mathias Agopianda27af92012-09-13 18:17:13 -0700205}
206
Dan Stoza9e56aa02015-11-02 13:00:03 -0800207void DisplayDevice::onSwapBuffersCompleted() const {
208 mDisplaySurface->onFrameCommitted();
209}
Mathias Agopianda27af92012-09-13 18:17:13 -0700210
Chia-I Wuf846a352017-11-10 09:22:52 -0800211bool DisplayDevice::makeCurrent() const {
Lloyd Pique144e1162017-12-20 16:44:52 -0800212 bool success = mFlinger->getRenderEngine().setCurrentSurface(*mSurface);
Mathias Agopian931bda12013-08-28 18:11:46 -0700213 setViewportAndProjection();
Chia-I Wuf846a352017-11-10 09:22:52 -0800214 return success;
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700215}
216
Mathias Agopian875d8e12013-06-07 15:35:48 -0700217void DisplayDevice::setViewportAndProjection() const {
218 size_t w = mDisplayWidth;
219 size_t h = mDisplayHeight;
Dan Stozac1879002014-05-22 15:59:05 -0700220 Rect sourceCrop(0, 0, w, h);
Riley Andrewsc3ebe662014-09-04 16:20:31 -0700221 mFlinger->getRenderEngine().setViewportAndProjection(w, h, sourceCrop, h,
222 false, Transform::ROT_0);
Mathias Agopianbae92d02012-09-28 01:00:47 -0700223}
224
Dan Stoza9e56aa02015-11-02 13:00:03 -0800225const sp<Fence>& DisplayDevice::getClientTargetAcquireFence() const {
226 return mDisplaySurface->getClientTargetAcquireFence();
227}
Dan Stoza9e56aa02015-11-02 13:00:03 -0800228
Mathias Agopian1b031492012-06-20 17:51:20 -0700229// ----------------------------------------------------------------------------
230
Mathias Agopian13127d82013-03-05 17:47:11 -0800231void DisplayDevice::setVisibleLayersSortedByZ(const Vector< sp<Layer> >& layers) {
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700232 mVisibleLayersSortedByZ = layers;
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700233}
234
Mathias Agopian13127d82013-03-05 17:47:11 -0800235const Vector< sp<Layer> >& DisplayDevice::getVisibleLayersSortedByZ() const {
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700236 return mVisibleLayersSortedByZ;
237}
238
Chia-I Wu83806892017-11-16 10:50:20 -0800239void DisplayDevice::setLayersNeedingFences(const Vector< sp<Layer> >& layers) {
240 mLayersNeedingFences = layers;
241}
242
243const Vector< sp<Layer> >& DisplayDevice::getLayersNeedingFences() const {
244 return mLayersNeedingFences;
245}
246
Mathias Agopiancd60f992012-08-16 16:28:27 -0700247Region DisplayDevice::getDirtyRegion(bool repaintEverything) const {
248 Region dirty;
Mathias Agopiancd60f992012-08-16 16:28:27 -0700249 if (repaintEverything) {
250 dirty.set(getBounds());
251 } else {
Mathias Agopianda8d0a52012-09-04 15:05:38 -0700252 const Transform& planeTransform(mGlobalTransform);
Mathias Agopiancd60f992012-08-16 16:28:27 -0700253 dirty = planeTransform.transform(this->dirtyRegion);
254 dirty.andSelf(getBounds());
255 }
256 return dirty;
257}
258
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700259// ----------------------------------------------------------------------------
Prashant Malani2c9b11f2014-05-25 01:36:31 -0700260void DisplayDevice::setPowerMode(int mode) {
261 mPowerMode = mode;
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700262}
263
Prashant Malani2c9b11f2014-05-25 01:36:31 -0700264int DisplayDevice::getPowerMode() const {
265 return mPowerMode;
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700266}
267
Prashant Malani2c9b11f2014-05-25 01:36:31 -0700268bool DisplayDevice::isDisplayOn() const {
269 return (mPowerMode != HWC_POWER_MODE_OFF);
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700270}
271
272// ----------------------------------------------------------------------------
Michael Lentine6c9e34a2014-07-14 13:48:55 -0700273void DisplayDevice::setActiveConfig(int mode) {
274 mActiveConfig = mode;
275}
276
277int DisplayDevice::getActiveConfig() const {
278 return mActiveConfig;
279}
280
281// ----------------------------------------------------------------------------
Peiyong Lina52f0292018-03-14 17:26:31 -0700282void DisplayDevice::setActiveColorMode(ColorMode mode) {
Michael Wright28f24d02016-07-12 13:30:53 -0700283 mActiveColorMode = mode;
284}
285
Peiyong Lina52f0292018-03-14 17:26:31 -0700286ColorMode DisplayDevice::getActiveColorMode() const {
Michael Wright28f24d02016-07-12 13:30:53 -0700287 return mActiveColorMode;
288}
Courtney Goeltzenleuchter79d27242017-07-13 17:54:01 -0600289
Peiyong Lindd9b2ae2018-03-01 16:22:45 -0800290RenderIntent DisplayDevice::getActiveRenderIntent() const {
291 return mActiveRenderIntent;
292}
293
294void DisplayDevice::setActiveRenderIntent(RenderIntent renderIntent) {
295 mActiveRenderIntent = renderIntent;
296}
297
Yiwei Zhang7c64f172018-03-07 14:52:28 -0800298void DisplayDevice::setColorTransform(const mat4& transform) {
299 const bool isIdentity = (transform == mat4());
300 mColorTransform =
301 isIdentity ? HAL_COLOR_TRANSFORM_IDENTITY : HAL_COLOR_TRANSFORM_ARBITRARY_MATRIX;
302}
303
304android_color_transform_t DisplayDevice::getColorTransform() const {
305 return mColorTransform;
306}
307
Peiyong Lin34beb7a2018-03-28 11:57:12 -0700308void DisplayDevice::setCompositionDataSpace(ui::Dataspace dataspace) {
Peiyong Lindd9b2ae2018-03-01 16:22:45 -0800309 mCompositionDataSpace = dataspace;
Courtney Goeltzenleuchter79d27242017-07-13 17:54:01 -0600310 ANativeWindow* const window = mNativeWindow.get();
Peiyong Lin34beb7a2018-03-28 11:57:12 -0700311 native_window_set_buffers_data_space(window, static_cast<android_dataspace>(dataspace));
Courtney Goeltzenleuchter79d27242017-07-13 17:54:01 -0600312}
Michael Wright28f24d02016-07-12 13:30:53 -0700313
Peiyong Lindd9b2ae2018-03-01 16:22:45 -0800314ui::Dataspace DisplayDevice::getCompositionDataSpace() const {
315 return mCompositionDataSpace;
316}
317
Michael Wright28f24d02016-07-12 13:30:53 -0700318// ----------------------------------------------------------------------------
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700319
Mathias Agopian28947d72012-08-08 18:51:15 -0700320void DisplayDevice::setLayerStack(uint32_t stack) {
321 mLayerStack = stack;
322 dirtyRegion.set(bounds());
323}
324
325// ----------------------------------------------------------------------------
326
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700327uint32_t DisplayDevice::getOrientationTransform() const {
328 uint32_t transform = 0;
329 switch (mOrientation) {
330 case DisplayState::eOrientationDefault:
331 transform = Transform::ROT_0;
332 break;
333 case DisplayState::eOrientation90:
334 transform = Transform::ROT_90;
335 break;
336 case DisplayState::eOrientation180:
337 transform = Transform::ROT_180;
338 break;
339 case DisplayState::eOrientation270:
340 transform = Transform::ROT_270;
341 break;
342 }
343 return transform;
344}
345
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700346status_t DisplayDevice::orientationToTransfrom(
Mathias Agopian1b031492012-06-20 17:51:20 -0700347 int orientation, int w, int h, Transform* tr)
348{
349 uint32_t flags = 0;
350 switch (orientation) {
Mathias Agopian3165cc22012-08-08 19:42:09 -0700351 case DisplayState::eOrientationDefault:
Mathias Agopian1b031492012-06-20 17:51:20 -0700352 flags = Transform::ROT_0;
353 break;
Mathias Agopian3165cc22012-08-08 19:42:09 -0700354 case DisplayState::eOrientation90:
Mathias Agopian1b031492012-06-20 17:51:20 -0700355 flags = Transform::ROT_90;
356 break;
Mathias Agopian3165cc22012-08-08 19:42:09 -0700357 case DisplayState::eOrientation180:
Mathias Agopian1b031492012-06-20 17:51:20 -0700358 flags = Transform::ROT_180;
359 break;
Mathias Agopian3165cc22012-08-08 19:42:09 -0700360 case DisplayState::eOrientation270:
Mathias Agopian1b031492012-06-20 17:51:20 -0700361 flags = Transform::ROT_270;
362 break;
363 default:
364 return BAD_VALUE;
365 }
366 tr->set(flags, w, h);
367 return NO_ERROR;
368}
369
Michael Lentine47e45402014-07-18 15:34:25 -0700370void DisplayDevice::setDisplaySize(const int newWidth, const int newHeight) {
371 dirtyRegion.set(getBounds());
372
Lloyd Pique144e1162017-12-20 16:44:52 -0800373 mSurface->setNativeWindow(nullptr);
Michael Lentinef2568de2014-08-20 10:51:23 -0700374
Michael Lentine47e45402014-07-18 15:34:25 -0700375 mDisplaySurface->resizeBuffers(newWidth, newHeight);
376
377 ANativeWindow* const window = mNativeWindow.get();
Lloyd Pique144e1162017-12-20 16:44:52 -0800378 mSurface->setNativeWindow(window);
379 mDisplayWidth = mSurface->queryWidth();
380 mDisplayHeight = mSurface->queryHeight();
Michael Lentine47e45402014-07-18 15:34:25 -0700381
382 LOG_FATAL_IF(mDisplayWidth != newWidth,
383 "Unable to set new width to %d", newWidth);
384 LOG_FATAL_IF(mDisplayHeight != newHeight,
385 "Unable to set new height to %d", newHeight);
386}
387
Mathias Agopian00e8c7a2012-09-04 19:30:46 -0700388void DisplayDevice::setProjection(int orientation,
Mathias Agopianf5f714a2013-02-26 16:54:05 -0800389 const Rect& newViewport, const Rect& newFrame) {
390 Rect viewport(newViewport);
391 Rect frame(newFrame);
392
393 const int w = mDisplayWidth;
394 const int h = mDisplayHeight;
395
396 Transform R;
397 DisplayDevice::orientationToTransfrom(orientation, w, h, &R);
398
399 if (!frame.isValid()) {
400 // the destination frame can be invalid if it has never been set,
401 // in that case we assume the whole display frame.
402 frame = Rect(w, h);
403 }
404
405 if (viewport.isEmpty()) {
406 // viewport can be invalid if it has never been set, in that case
407 // we assume the whole display size.
408 // it's also invalid to have an empty viewport, so we handle that
409 // case in the same way.
410 viewport = Rect(w, h);
411 if (R.getOrientation() & Transform::ROT_90) {
412 // viewport is always specified in the logical orientation
413 // of the display (ie: post-rotation).
414 swap(viewport.right, viewport.bottom);
415 }
416 }
417
418 dirtyRegion.set(getBounds());
419
420 Transform TL, TP, S;
421 float src_width = viewport.width();
422 float src_height = viewport.height();
423 float dst_width = frame.width();
424 float dst_height = frame.height();
425 if (src_width != dst_width || src_height != dst_height) {
426 float sx = dst_width / src_width;
427 float sy = dst_height / src_height;
428 S.set(sx, 0, 0, sy);
429 }
430
431 float src_x = viewport.left;
432 float src_y = viewport.top;
433 float dst_x = frame.left;
434 float dst_y = frame.top;
435 TL.set(-src_x, -src_y);
436 TP.set(dst_x, dst_y);
437
438 // The viewport and frame are both in the logical orientation.
439 // Apply the logical translation, scale to physical size, apply the
440 // physical translation and finally rotate to the physical orientation.
441 mGlobalTransform = R * TP * S * TL;
442
443 const uint8_t type = mGlobalTransform.getType();
444 mNeedsFiltering = (!mGlobalTransform.preserveRects() ||
445 (type >= Transform::SCALE));
446
447 mScissor = mGlobalTransform.transform(viewport);
448 if (mScissor.isEmpty()) {
Mathias Agopian6c7f25a2013-05-09 20:37:10 -0700449 mScissor = getBounds();
Mathias Agopianf5f714a2013-02-26 16:54:05 -0800450 }
451
Mathias Agopianda8d0a52012-09-04 15:05:38 -0700452 mOrientation = orientation;
Pablo Ceballos021623b2016-04-15 17:31:51 -0700453 if (mType == DisplayType::DISPLAY_PRIMARY) {
454 uint32_t transform = 0;
455 switch (mOrientation) {
456 case DisplayState::eOrientationDefault:
457 transform = Transform::ROT_0;
458 break;
459 case DisplayState::eOrientation90:
460 transform = Transform::ROT_90;
461 break;
462 case DisplayState::eOrientation180:
463 transform = Transform::ROT_180;
464 break;
465 case DisplayState::eOrientation270:
466 transform = Transform::ROT_270;
467 break;
468 }
469 sPrimaryDisplayOrientation = transform;
470 }
Mathias Agopian00e8c7a2012-09-04 19:30:46 -0700471 mViewport = viewport;
472 mFrame = frame;
Mathias Agopian1b031492012-06-20 17:51:20 -0700473}
Mathias Agopian1d12d8a2012-09-18 01:38:00 -0700474
Pablo Ceballos021623b2016-04-15 17:31:51 -0700475uint32_t DisplayDevice::getPrimaryDisplayOrientationTransform() {
476 return sPrimaryDisplayOrientation;
477}
478
Mathias Agopian74d211a2013-04-22 16:55:35 +0200479void DisplayDevice::dump(String8& result) const {
Mathias Agopian1d12d8a2012-09-18 01:38:00 -0700480 const Transform& tr(mGlobalTransform);
Courtney Goeltzenleuchter152279d2017-08-14 18:18:30 -0600481 ANativeWindow* const window = mNativeWindow.get();
Courtney Goeltzenleuchter0ebaac32017-04-13 12:17:03 -0600482 result.appendFormat("+ DisplayDevice: %s\n", mDisplayName.string());
483 result.appendFormat(" type=%x, hwcId=%d, layerStack=%u, (%4dx%4d), ANativeWindow=%p "
484 "(%d:%d:%d:%d), orient=%2d (type=%08x), "
485 "flips=%u, isSecure=%d, powerMode=%d, activeConfig=%d, numLayers=%zu\n",
Courtney Goeltzenleuchter152279d2017-08-14 18:18:30 -0600486 mType, mHwcDisplayId, mLayerStack, mDisplayWidth, mDisplayHeight, window,
Lloyd Pique144e1162017-12-20 16:44:52 -0800487 mSurface->queryRedSize(), mSurface->queryGreenSize(),
488 mSurface->queryBlueSize(), mSurface->queryAlphaSize(), mOrientation,
489 tr.getType(), getPageFlipCount(), mIsSecure, mPowerMode, mActiveConfig,
Courtney Goeltzenleuchter0ebaac32017-04-13 12:17:03 -0600490 mVisibleLayersSortedByZ.size());
491 result.appendFormat(" v:[%d,%d,%d,%d], f:[%d,%d,%d,%d], s:[%d,%d,%d,%d],"
492 "transform:[[%0.3f,%0.3f,%0.3f][%0.3f,%0.3f,%0.3f][%0.3f,%0.3f,%0.3f]]\n",
493 mViewport.left, mViewport.top, mViewport.right, mViewport.bottom,
494 mFrame.left, mFrame.top, mFrame.right, mFrame.bottom, mScissor.left,
495 mScissor.top, mScissor.right, mScissor.bottom, tr[0][0], tr[1][0], tr[2][0],
496 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 -0600497 auto const surface = static_cast<Surface*>(window);
Peiyong Lin34beb7a2018-03-28 11:57:12 -0700498 ui::Dataspace dataspace = surface->getBuffersDataSpace();
Peiyong Lindd9b2ae2018-03-01 16:22:45 -0800499 result.appendFormat(" wideColorGamut=%d, hdr10=%d, colorMode=%s, dataspace: %s (%d)\n",
500 mHasWideColorGamut, mHasHdr10,
Chia-I Wu1e043612018-03-01 09:45:09 -0800501 decodeColorMode(mActiveColorMode).c_str(),
Peiyong Lin34beb7a2018-03-28 11:57:12 -0700502 dataspaceDetails(static_cast<android_dataspace>(dataspace)).c_str(), dataspace);
Mathias Agopian1d12d8a2012-09-18 01:38:00 -0700503
Jesse Hall99c7dbb2013-03-14 14:29:29 -0700504 String8 surfaceDump;
Dan Stozaf10c46e2014-11-11 10:32:31 -0800505 mDisplaySurface->dumpAsString(surfaceDump);
Jesse Hall99c7dbb2013-03-14 14:29:29 -0700506 result.append(surfaceDump);
Mathias Agopian1d12d8a2012-09-18 01:38:00 -0700507}
Irvelffc9efc2016-07-27 15:16:37 -0700508
509std::atomic<int32_t> DisplayDeviceState::nextDisplayId(1);
510
511DisplayDeviceState::DisplayDeviceState(DisplayDevice::DisplayType type, bool isSecure)
512 : type(type),
513 layerStack(DisplayDevice::NO_LAYER_STACK),
514 orientation(0),
515 width(0),
516 height(0),
517 isSecure(isSecure)
518{
519 viewport.makeInvalid();
520 frame.makeInvalid();
521}
Peiyong Linfd997e02018-03-28 15:29:00 -0700522
523} // namespace android