blob: 00f8cc9c5c4f4b0eedb722197867f22f021ae04c [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 Lin2c327ac2018-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 Lin2c327ac2018-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 Linfb069302018-04-25 14:34:31 -0700111 std::vector<Hdr> types = hdrCapabilities.getSupportedHdrTypes();
112 for (Hdr hdrType : types) {
Peiyong Lin62665892018-04-16 11:07:44 -0700113 switch (hdrType) {
114 case Hdr::HDR10:
115 mHasHdr10 = true;
116 break;
117 case Hdr::HLG:
118 mHasHLG = true;
119 break;
120 case Hdr::DOLBY_VISION:
121 mHasDolbyVision = true;
122 break;
123 default:
124 ALOGE("UNKNOWN HDR capability: %d", static_cast<int32_t>(hdrType));
125 }
126 }
Jesse Hallffe1f192013-03-22 15:13:48 -0700127
Peiyong Linfb069302018-04-25 14:34:31 -0700128 float minLuminance = hdrCapabilities.getDesiredMinLuminance();
129 float maxLuminance = hdrCapabilities.getDesiredMaxLuminance();
130 float maxAverageLuminance = hdrCapabilities.getDesiredMaxAverageLuminance();
131
132 minLuminance = minLuminance <= 0.0 ? sDefaultMinLumiance : minLuminance;
133 maxLuminance = maxLuminance <= 0.0 ? sDefaultMaxLumiance : maxLuminance;
134 maxAverageLuminance = maxAverageLuminance <= 0.0 ? sDefaultMaxLumiance : maxAverageLuminance;
135 if (this->hasWideColorGamut()) {
136 // insert HDR10/HLG as we will force client composition for HDR10/HLG
137 // layers
138 if (!hasHDR10Support()) {
139 types.push_back(Hdr::HDR10);
140 }
141
142 if (!hasHLGSupport()) {
143 types.push_back(Hdr::HLG);
144 }
145 }
146 mHdrCapabilities = HdrCapabilities(types, maxLuminance, maxAverageLuminance, minLuminance);
147
Jesse Hallffe1f192013-03-22 15:13:48 -0700148 // initialize the display orientation transform.
149 setProjection(DisplayState::eOrientationDefault, mViewport, mFrame);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800150}
151
Lloyd Pique09594832018-01-22 17:48:03 -0800152DisplayDevice::~DisplayDevice() = default;
Mathias Agopian92a979a2012-08-02 18:32:23 -0700153
Jesse Hall02d86562013-03-25 14:43:23 -0700154void DisplayDevice::disconnect(HWComposer& hwc) {
155 if (mHwcDisplayId >= 0) {
156 hwc.disconnectDisplay(mHwcDisplayId);
Jesse Hall02d86562013-03-25 14:43:23 -0700157 mHwcDisplayId = -1;
158 }
159}
160
Mathias Agopian92a979a2012-08-02 18:32:23 -0700161bool DisplayDevice::isValid() const {
Peiyong Lin566a3b42018-01-09 18:22:43 -0800162 return mFlinger != nullptr;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800163}
164
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700165int DisplayDevice::getWidth() const {
Mathias Agopiana4912602012-07-12 14:25:33 -0700166 return mDisplayWidth;
167}
168
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700169int DisplayDevice::getHeight() const {
Mathias Agopiana4912602012-07-12 14:25:33 -0700170 return mDisplayHeight;
171}
172
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700173void DisplayDevice::setDisplayName(const String8& displayName) {
174 if (!displayName.isEmpty()) {
175 // never override the name with an empty name
176 mDisplayName = displayName;
177 }
178}
179
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700180uint32_t DisplayDevice::getPageFlipCount() const {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700181 return mPageFlipCount;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800182}
183
Chia-I Wub02087d2017-11-09 10:19:54 -0800184void DisplayDevice::flip() const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800185{
Mathias Agopian875d8e12013-06-07 15:35:48 -0700186 mFlinger->getRenderEngine().checkErrors();
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700187 mPageFlipCount++;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800188}
189
Dan Stoza71433162014-02-04 16:22:36 -0800190status_t DisplayDevice::beginFrame(bool mustRecompose) const {
191 return mDisplaySurface->beginFrame(mustRecompose);
Jesse Hall028dc8f2013-08-20 16:35:32 -0700192}
193
Dan Stoza9e56aa02015-11-02 13:00:03 -0800194status_t DisplayDevice::prepareFrame(HWComposer& hwc) {
195 status_t error = hwc.prepare(*this);
196 if (error != NO_ERROR) {
197 return error;
198 }
199
200 DisplaySurface::CompositionType compositionType;
201 bool hasClient = hwc.hasClientComposition(mHwcDisplayId);
202 bool hasDevice = hwc.hasDeviceComposition(mHwcDisplayId);
203 if (hasClient && hasDevice) {
204 compositionType = DisplaySurface::COMPOSITION_MIXED;
205 } else if (hasClient) {
206 compositionType = DisplaySurface::COMPOSITION_GLES;
207 } else if (hasDevice) {
208 compositionType = DisplaySurface::COMPOSITION_HWC;
209 } else {
210 // Nothing to do -- when turning the screen off we get a frame like
211 // this. Call it a HWC frame since we won't be doing any GLES work but
212 // will do a prepare/set cycle.
213 compositionType = DisplaySurface::COMPOSITION_HWC;
214 }
215 return mDisplaySurface->prepareFrame(compositionType);
216}
Jesse Hall38efe862013-04-06 23:12:29 -0700217
Mathias Agopianda27af92012-09-13 18:17:13 -0700218void DisplayDevice::swapBuffers(HWComposer& hwc) const {
Madhuri Athota88a905b2017-05-04 16:58:15 +0530219 if (hwc.hasClientComposition(mHwcDisplayId) || hwc.hasFlipClientTargetRequest(mHwcDisplayId)) {
Lloyd Pique144e1162017-12-20 16:44:52 -0800220 mSurface->swapBuffers();
Mathias Agopianda27af92012-09-13 18:17:13 -0700221 }
Mathias Agopian52e21482012-09-24 18:07:21 -0700222
Jesse Hall99c7dbb2013-03-14 14:29:29 -0700223 status_t result = mDisplaySurface->advanceFrame();
224 if (result != NO_ERROR) {
225 ALOGE("[%s] failed pushing new frame to HWC: %d",
226 mDisplayName.string(), result);
Mathias Agopian32341382012-09-25 19:16:28 -0700227 }
Mathias Agopianda27af92012-09-13 18:17:13 -0700228}
229
Dan Stoza9e56aa02015-11-02 13:00:03 -0800230void DisplayDevice::onSwapBuffersCompleted() const {
231 mDisplaySurface->onFrameCommitted();
232}
Mathias Agopianda27af92012-09-13 18:17:13 -0700233
Chia-I Wuf846a352017-11-10 09:22:52 -0800234bool DisplayDevice::makeCurrent() const {
Lloyd Pique144e1162017-12-20 16:44:52 -0800235 bool success = mFlinger->getRenderEngine().setCurrentSurface(*mSurface);
Mathias Agopian931bda12013-08-28 18:11:46 -0700236 setViewportAndProjection();
Chia-I Wuf846a352017-11-10 09:22:52 -0800237 return success;
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700238}
239
Mathias Agopian875d8e12013-06-07 15:35:48 -0700240void DisplayDevice::setViewportAndProjection() const {
241 size_t w = mDisplayWidth;
242 size_t h = mDisplayHeight;
Dan Stozac1879002014-05-22 15:59:05 -0700243 Rect sourceCrop(0, 0, w, h);
Riley Andrewsc3ebe662014-09-04 16:20:31 -0700244 mFlinger->getRenderEngine().setViewportAndProjection(w, h, sourceCrop, h,
245 false, Transform::ROT_0);
Mathias Agopianbae92d02012-09-28 01:00:47 -0700246}
247
Dan Stoza9e56aa02015-11-02 13:00:03 -0800248const sp<Fence>& DisplayDevice::getClientTargetAcquireFence() const {
249 return mDisplaySurface->getClientTargetAcquireFence();
250}
Dan Stoza9e56aa02015-11-02 13:00:03 -0800251
Mathias Agopian1b031492012-06-20 17:51:20 -0700252// ----------------------------------------------------------------------------
253
Mathias Agopian13127d82013-03-05 17:47:11 -0800254void DisplayDevice::setVisibleLayersSortedByZ(const Vector< sp<Layer> >& layers) {
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700255 mVisibleLayersSortedByZ = layers;
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700256}
257
Mathias Agopian13127d82013-03-05 17:47:11 -0800258const Vector< sp<Layer> >& DisplayDevice::getVisibleLayersSortedByZ() const {
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700259 return mVisibleLayersSortedByZ;
260}
261
Chia-I Wu83806892017-11-16 10:50:20 -0800262void DisplayDevice::setLayersNeedingFences(const Vector< sp<Layer> >& layers) {
263 mLayersNeedingFences = layers;
264}
265
266const Vector< sp<Layer> >& DisplayDevice::getLayersNeedingFences() const {
267 return mLayersNeedingFences;
268}
269
Mathias Agopiancd60f992012-08-16 16:28:27 -0700270Region DisplayDevice::getDirtyRegion(bool repaintEverything) const {
271 Region dirty;
Mathias Agopiancd60f992012-08-16 16:28:27 -0700272 if (repaintEverything) {
273 dirty.set(getBounds());
274 } else {
Mathias Agopianda8d0a52012-09-04 15:05:38 -0700275 const Transform& planeTransform(mGlobalTransform);
Mathias Agopiancd60f992012-08-16 16:28:27 -0700276 dirty = planeTransform.transform(this->dirtyRegion);
277 dirty.andSelf(getBounds());
278 }
279 return dirty;
280}
281
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700282// ----------------------------------------------------------------------------
Prashant Malani2c9b11f2014-05-25 01:36:31 -0700283void DisplayDevice::setPowerMode(int mode) {
284 mPowerMode = mode;
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700285}
286
Prashant Malani2c9b11f2014-05-25 01:36:31 -0700287int DisplayDevice::getPowerMode() const {
288 return mPowerMode;
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700289}
290
Prashant Malani2c9b11f2014-05-25 01:36:31 -0700291bool DisplayDevice::isDisplayOn() const {
292 return (mPowerMode != HWC_POWER_MODE_OFF);
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700293}
294
295// ----------------------------------------------------------------------------
Michael Lentine6c9e34a2014-07-14 13:48:55 -0700296void DisplayDevice::setActiveConfig(int mode) {
297 mActiveConfig = mode;
298}
299
300int DisplayDevice::getActiveConfig() const {
301 return mActiveConfig;
302}
303
304// ----------------------------------------------------------------------------
Peiyong Lina52f0292018-03-14 17:26:31 -0700305void DisplayDevice::setActiveColorMode(ColorMode mode) {
Michael Wright28f24d02016-07-12 13:30:53 -0700306 mActiveColorMode = mode;
307}
308
Peiyong Lina52f0292018-03-14 17:26:31 -0700309ColorMode DisplayDevice::getActiveColorMode() const {
Michael Wright28f24d02016-07-12 13:30:53 -0700310 return mActiveColorMode;
311}
Courtney Goeltzenleuchter79d27242017-07-13 17:54:01 -0600312
Peiyong Lindd9b2ae2018-03-01 16:22:45 -0800313RenderIntent DisplayDevice::getActiveRenderIntent() const {
314 return mActiveRenderIntent;
315}
316
317void DisplayDevice::setActiveRenderIntent(RenderIntent renderIntent) {
318 mActiveRenderIntent = renderIntent;
319}
320
Yiwei Zhang7c64f172018-03-07 14:52:28 -0800321void DisplayDevice::setColorTransform(const mat4& transform) {
322 const bool isIdentity = (transform == mat4());
323 mColorTransform =
324 isIdentity ? HAL_COLOR_TRANSFORM_IDENTITY : HAL_COLOR_TRANSFORM_ARBITRARY_MATRIX;
325}
326
327android_color_transform_t DisplayDevice::getColorTransform() const {
328 return mColorTransform;
329}
330
Peiyong Lin34beb7a2018-03-28 11:57:12 -0700331void DisplayDevice::setCompositionDataSpace(ui::Dataspace dataspace) {
Peiyong Lindd9b2ae2018-03-01 16:22:45 -0800332 mCompositionDataSpace = dataspace;
Courtney Goeltzenleuchter79d27242017-07-13 17:54:01 -0600333 ANativeWindow* const window = mNativeWindow.get();
Peiyong Lin34beb7a2018-03-28 11:57:12 -0700334 native_window_set_buffers_data_space(window, static_cast<android_dataspace>(dataspace));
Courtney Goeltzenleuchter79d27242017-07-13 17:54:01 -0600335}
Michael Wright28f24d02016-07-12 13:30:53 -0700336
Peiyong Lindd9b2ae2018-03-01 16:22:45 -0800337ui::Dataspace DisplayDevice::getCompositionDataSpace() const {
338 return mCompositionDataSpace;
339}
340
Michael Wright28f24d02016-07-12 13:30:53 -0700341// ----------------------------------------------------------------------------
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700342
Mathias Agopian28947d72012-08-08 18:51:15 -0700343void DisplayDevice::setLayerStack(uint32_t stack) {
344 mLayerStack = stack;
345 dirtyRegion.set(bounds());
346}
347
348// ----------------------------------------------------------------------------
349
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700350uint32_t DisplayDevice::getOrientationTransform() const {
351 uint32_t transform = 0;
352 switch (mOrientation) {
353 case DisplayState::eOrientationDefault:
354 transform = Transform::ROT_0;
355 break;
356 case DisplayState::eOrientation90:
357 transform = Transform::ROT_90;
358 break;
359 case DisplayState::eOrientation180:
360 transform = Transform::ROT_180;
361 break;
362 case DisplayState::eOrientation270:
363 transform = Transform::ROT_270;
364 break;
365 }
366 return transform;
367}
368
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700369status_t DisplayDevice::orientationToTransfrom(
Mathias Agopian1b031492012-06-20 17:51:20 -0700370 int orientation, int w, int h, Transform* tr)
371{
372 uint32_t flags = 0;
373 switch (orientation) {
Mathias Agopian3165cc22012-08-08 19:42:09 -0700374 case DisplayState::eOrientationDefault:
Mathias Agopian1b031492012-06-20 17:51:20 -0700375 flags = Transform::ROT_0;
376 break;
Mathias Agopian3165cc22012-08-08 19:42:09 -0700377 case DisplayState::eOrientation90:
Mathias Agopian1b031492012-06-20 17:51:20 -0700378 flags = Transform::ROT_90;
379 break;
Mathias Agopian3165cc22012-08-08 19:42:09 -0700380 case DisplayState::eOrientation180:
Mathias Agopian1b031492012-06-20 17:51:20 -0700381 flags = Transform::ROT_180;
382 break;
Mathias Agopian3165cc22012-08-08 19:42:09 -0700383 case DisplayState::eOrientation270:
Mathias Agopian1b031492012-06-20 17:51:20 -0700384 flags = Transform::ROT_270;
385 break;
386 default:
387 return BAD_VALUE;
388 }
389 tr->set(flags, w, h);
390 return NO_ERROR;
391}
392
Michael Lentine47e45402014-07-18 15:34:25 -0700393void DisplayDevice::setDisplaySize(const int newWidth, const int newHeight) {
394 dirtyRegion.set(getBounds());
395
Lloyd Pique144e1162017-12-20 16:44:52 -0800396 mSurface->setNativeWindow(nullptr);
Michael Lentinef2568de2014-08-20 10:51:23 -0700397
Michael Lentine47e45402014-07-18 15:34:25 -0700398 mDisplaySurface->resizeBuffers(newWidth, newHeight);
399
400 ANativeWindow* const window = mNativeWindow.get();
Lloyd Pique144e1162017-12-20 16:44:52 -0800401 mSurface->setNativeWindow(window);
402 mDisplayWidth = mSurface->queryWidth();
403 mDisplayHeight = mSurface->queryHeight();
Michael Lentine47e45402014-07-18 15:34:25 -0700404
405 LOG_FATAL_IF(mDisplayWidth != newWidth,
406 "Unable to set new width to %d", newWidth);
407 LOG_FATAL_IF(mDisplayHeight != newHeight,
408 "Unable to set new height to %d", newHeight);
409}
410
Mathias Agopian00e8c7a2012-09-04 19:30:46 -0700411void DisplayDevice::setProjection(int orientation,
Mathias Agopianf5f714a2013-02-26 16:54:05 -0800412 const Rect& newViewport, const Rect& newFrame) {
413 Rect viewport(newViewport);
414 Rect frame(newFrame);
415
416 const int w = mDisplayWidth;
417 const int h = mDisplayHeight;
418
419 Transform R;
420 DisplayDevice::orientationToTransfrom(orientation, w, h, &R);
421
422 if (!frame.isValid()) {
423 // the destination frame can be invalid if it has never been set,
424 // in that case we assume the whole display frame.
425 frame = Rect(w, h);
426 }
427
428 if (viewport.isEmpty()) {
429 // viewport can be invalid if it has never been set, in that case
430 // we assume the whole display size.
431 // it's also invalid to have an empty viewport, so we handle that
432 // case in the same way.
433 viewport = Rect(w, h);
434 if (R.getOrientation() & Transform::ROT_90) {
435 // viewport is always specified in the logical orientation
436 // of the display (ie: post-rotation).
437 swap(viewport.right, viewport.bottom);
438 }
439 }
440
441 dirtyRegion.set(getBounds());
442
443 Transform TL, TP, S;
444 float src_width = viewport.width();
445 float src_height = viewport.height();
446 float dst_width = frame.width();
447 float dst_height = frame.height();
448 if (src_width != dst_width || src_height != dst_height) {
449 float sx = dst_width / src_width;
450 float sy = dst_height / src_height;
451 S.set(sx, 0, 0, sy);
452 }
453
454 float src_x = viewport.left;
455 float src_y = viewport.top;
456 float dst_x = frame.left;
457 float dst_y = frame.top;
458 TL.set(-src_x, -src_y);
459 TP.set(dst_x, dst_y);
460
461 // The viewport and frame are both in the logical orientation.
462 // Apply the logical translation, scale to physical size, apply the
463 // physical translation and finally rotate to the physical orientation.
464 mGlobalTransform = R * TP * S * TL;
465
466 const uint8_t type = mGlobalTransform.getType();
467 mNeedsFiltering = (!mGlobalTransform.preserveRects() ||
468 (type >= Transform::SCALE));
469
470 mScissor = mGlobalTransform.transform(viewport);
471 if (mScissor.isEmpty()) {
Mathias Agopian6c7f25a2013-05-09 20:37:10 -0700472 mScissor = getBounds();
Mathias Agopianf5f714a2013-02-26 16:54:05 -0800473 }
474
Mathias Agopianda8d0a52012-09-04 15:05:38 -0700475 mOrientation = orientation;
Pablo Ceballos021623b2016-04-15 17:31:51 -0700476 if (mType == DisplayType::DISPLAY_PRIMARY) {
477 uint32_t transform = 0;
478 switch (mOrientation) {
479 case DisplayState::eOrientationDefault:
480 transform = Transform::ROT_0;
481 break;
482 case DisplayState::eOrientation90:
483 transform = Transform::ROT_90;
484 break;
485 case DisplayState::eOrientation180:
486 transform = Transform::ROT_180;
487 break;
488 case DisplayState::eOrientation270:
489 transform = Transform::ROT_270;
490 break;
491 }
492 sPrimaryDisplayOrientation = transform;
493 }
Mathias Agopian00e8c7a2012-09-04 19:30:46 -0700494 mViewport = viewport;
495 mFrame = frame;
Mathias Agopian1b031492012-06-20 17:51:20 -0700496}
Mathias Agopian1d12d8a2012-09-18 01:38:00 -0700497
Pablo Ceballos021623b2016-04-15 17:31:51 -0700498uint32_t DisplayDevice::getPrimaryDisplayOrientationTransform() {
499 return sPrimaryDisplayOrientation;
500}
501
Mathias Agopian74d211a2013-04-22 16:55:35 +0200502void DisplayDevice::dump(String8& result) const {
Mathias Agopian1d12d8a2012-09-18 01:38:00 -0700503 const Transform& tr(mGlobalTransform);
Courtney Goeltzenleuchter152279d2017-08-14 18:18:30 -0600504 ANativeWindow* const window = mNativeWindow.get();
Courtney Goeltzenleuchter0ebaac32017-04-13 12:17:03 -0600505 result.appendFormat("+ DisplayDevice: %s\n", mDisplayName.string());
506 result.appendFormat(" type=%x, hwcId=%d, layerStack=%u, (%4dx%4d), ANativeWindow=%p "
507 "(%d:%d:%d:%d), orient=%2d (type=%08x), "
508 "flips=%u, isSecure=%d, powerMode=%d, activeConfig=%d, numLayers=%zu\n",
Courtney Goeltzenleuchter152279d2017-08-14 18:18:30 -0600509 mType, mHwcDisplayId, mLayerStack, mDisplayWidth, mDisplayHeight, window,
Lloyd Pique144e1162017-12-20 16:44:52 -0800510 mSurface->queryRedSize(), mSurface->queryGreenSize(),
511 mSurface->queryBlueSize(), mSurface->queryAlphaSize(), mOrientation,
512 tr.getType(), getPageFlipCount(), mIsSecure, mPowerMode, mActiveConfig,
Courtney Goeltzenleuchter0ebaac32017-04-13 12:17:03 -0600513 mVisibleLayersSortedByZ.size());
514 result.appendFormat(" v:[%d,%d,%d,%d], f:[%d,%d,%d,%d], s:[%d,%d,%d,%d],"
515 "transform:[[%0.3f,%0.3f,%0.3f][%0.3f,%0.3f,%0.3f][%0.3f,%0.3f,%0.3f]]\n",
516 mViewport.left, mViewport.top, mViewport.right, mViewport.bottom,
517 mFrame.left, mFrame.top, mFrame.right, mFrame.bottom, mScissor.left,
518 mScissor.top, mScissor.right, mScissor.bottom, tr[0][0], tr[1][0], tr[2][0],
519 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 -0600520 auto const surface = static_cast<Surface*>(window);
Peiyong Lin34beb7a2018-03-28 11:57:12 -0700521 ui::Dataspace dataspace = surface->getBuffersDataSpace();
Peiyong Lindd9b2ae2018-03-01 16:22:45 -0800522 result.appendFormat(" wideColorGamut=%d, hdr10=%d, colorMode=%s, dataspace: %s (%d)\n",
523 mHasWideColorGamut, mHasHdr10,
Chia-I Wu1e043612018-03-01 09:45:09 -0800524 decodeColorMode(mActiveColorMode).c_str(),
Peiyong Lin34beb7a2018-03-28 11:57:12 -0700525 dataspaceDetails(static_cast<android_dataspace>(dataspace)).c_str(), dataspace);
Mathias Agopian1d12d8a2012-09-18 01:38:00 -0700526
Jesse Hall99c7dbb2013-03-14 14:29:29 -0700527 String8 surfaceDump;
Dan Stozaf10c46e2014-11-11 10:32:31 -0800528 mDisplaySurface->dumpAsString(surfaceDump);
Jesse Hall99c7dbb2013-03-14 14:29:29 -0700529 result.append(surfaceDump);
Mathias Agopian1d12d8a2012-09-18 01:38:00 -0700530}
Irvelffc9efc2016-07-27 15:16:37 -0700531
532std::atomic<int32_t> DisplayDeviceState::nextDisplayId(1);
533
534DisplayDeviceState::DisplayDeviceState(DisplayDevice::DisplayType type, bool isSecure)
535 : type(type),
536 layerStack(DisplayDevice::NO_LAYER_STACK),
537 orientation(0),
538 width(0),
539 height(0),
540 isSecure(isSecure)
541{
542 viewport.makeInvalid();
543 frame.makeInvalid();
544}
Peiyong Linfd997e02018-03-28 15:29:00 -0700545
546} // namespace android