blob: 1d3c003f1aeb998c8ddea42f5f4ad13792325b95 [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,
Peiyong Lin136fbbc2018-04-17 15:09:44 -070083 const std::unordered_map<ui::ColorMode, std::vector<ui::RenderIntent>>& hdrAndRenderIntents,
Lloyd Pique09594832018-01-22 17:48:03 -080084 int initialPowerMode)
Jesse Hallb7a05492014-08-14 15:45:06 -070085 : lastCompositionHadVisibleLayers(false),
86 mFlinger(flinger),
Dan Stoza9e56aa02015-11-02 13:00:03 -080087 mType(type),
88 mHwcDisplayId(hwcId),
Chih-Wei Huang27e25622013-01-07 17:33:56 +080089 mDisplayToken(displayToken),
Lloyd Pique09594832018-01-22 17:48:03 -080090 mNativeWindow(nativeWindow),
Jesse Hall99c7dbb2013-03-14 14:29:29 -070091 mDisplaySurface(displaySurface),
Lloyd Pique09594832018-01-22 17:48:03 -080092 mSurface{std::move(renderSurface)},
93 mDisplayWidth(displayWidth),
94 mDisplayHeight(displayHeight),
95 mPageFlipCount(0),
Jamie Gennisdd3cb842012-10-19 18:19:11 -070096 mIsSecure(isSecure),
Jesse Hall01e29052013-02-19 16:13:35 -080097 mLayerStack(NO_LAYER_STACK),
Prashant Malani2c9b11f2014-05-25 01:36:31 -070098 mOrientation(),
Lloyd Pique09594832018-01-22 17:48:03 -080099 mViewport(Rect::INVALID_RECT),
100 mFrame(Rect::INVALID_RECT),
101 mPowerMode(initialPowerMode),
102 mActiveConfig(0),
103 mActiveColorMode(ColorMode::NATIVE),
Yiwei Zhang7c64f172018-03-07 14:52:28 -0800104 mColorTransform(HAL_COLOR_TRANSFORM_IDENTITY),
Peiyong Lindd9b2ae2018-03-01 16:22:45 -0800105 mHasWideColorGamut(hasWideColorGamut),
Peiyong Lin62665892018-04-16 11:07:44 -0700106 mHasHdr10(false),
107 mHasHLG(false),
Peiyong Lin0ac5f4e2018-04-19 22:06:34 -0700108 mHasDolbyVision(false),
Peiyong Lin136fbbc2018-04-17 15:09:44 -0700109 mSupportedPerFrameMetadata(supportedPerFrameMetadata),
110 mHasBT2100PQColorimetric(false),
111 mHasBT2100PQEnhance(false),
112 mHasBT2100HLGColorimetric(false),
113 mHasBT2100HLGEnhance(false)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800114{
Courtney Goeltzenleuchter5d943892017-03-22 13:46:46 -0600115 // clang-format on
Peiyong Linfb069302018-04-25 14:34:31 -0700116 std::vector<Hdr> types = hdrCapabilities.getSupportedHdrTypes();
117 for (Hdr hdrType : types) {
Peiyong Lin62665892018-04-16 11:07:44 -0700118 switch (hdrType) {
119 case Hdr::HDR10:
120 mHasHdr10 = true;
121 break;
122 case Hdr::HLG:
123 mHasHLG = true;
124 break;
125 case Hdr::DOLBY_VISION:
126 mHasDolbyVision = true;
127 break;
128 default:
129 ALOGE("UNKNOWN HDR capability: %d", static_cast<int32_t>(hdrType));
130 }
131 }
Jesse Hallffe1f192013-03-22 15:13:48 -0700132
Peiyong Linfb069302018-04-25 14:34:31 -0700133 float minLuminance = hdrCapabilities.getDesiredMinLuminance();
134 float maxLuminance = hdrCapabilities.getDesiredMaxLuminance();
135 float maxAverageLuminance = hdrCapabilities.getDesiredMaxAverageLuminance();
136
137 minLuminance = minLuminance <= 0.0 ? sDefaultMinLumiance : minLuminance;
138 maxLuminance = maxLuminance <= 0.0 ? sDefaultMaxLumiance : maxLuminance;
139 maxAverageLuminance = maxAverageLuminance <= 0.0 ? sDefaultMaxLumiance : maxAverageLuminance;
140 if (this->hasWideColorGamut()) {
141 // insert HDR10/HLG as we will force client composition for HDR10/HLG
142 // layers
143 if (!hasHDR10Support()) {
144 types.push_back(Hdr::HDR10);
145 }
146
147 if (!hasHLGSupport()) {
148 types.push_back(Hdr::HLG);
149 }
150 }
151 mHdrCapabilities = HdrCapabilities(types, maxLuminance, maxAverageLuminance, minLuminance);
152
Peiyong Lin136fbbc2018-04-17 15:09:44 -0700153 auto iter = hdrAndRenderIntents.find(ColorMode::BT2100_PQ);
154 if (iter != hdrAndRenderIntents.end()) {
155 hasToneMapping(iter->second,
156 &mHasBT2100PQColorimetric, &mHasBT2100PQEnhance);
157 }
158
159 iter = hdrAndRenderIntents.find(ColorMode::BT2100_HLG);
160 if (iter != hdrAndRenderIntents.end()) {
161 hasToneMapping(iter->second,
162 &mHasBT2100HLGColorimetric, &mHasBT2100HLGEnhance);
163 }
164
Jesse Hallffe1f192013-03-22 15:13:48 -0700165 // initialize the display orientation transform.
166 setProjection(DisplayState::eOrientationDefault, mViewport, mFrame);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800167}
168
Lloyd Pique09594832018-01-22 17:48:03 -0800169DisplayDevice::~DisplayDevice() = default;
Mathias Agopian92a979a2012-08-02 18:32:23 -0700170
Jesse Hall02d86562013-03-25 14:43:23 -0700171void DisplayDevice::disconnect(HWComposer& hwc) {
172 if (mHwcDisplayId >= 0) {
173 hwc.disconnectDisplay(mHwcDisplayId);
Jesse Hall02d86562013-03-25 14:43:23 -0700174 mHwcDisplayId = -1;
175 }
176}
177
Mathias Agopian92a979a2012-08-02 18:32:23 -0700178bool DisplayDevice::isValid() const {
Peiyong Lin566a3b42018-01-09 18:22:43 -0800179 return mFlinger != nullptr;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800180}
181
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700182int DisplayDevice::getWidth() const {
Mathias Agopiana4912602012-07-12 14:25:33 -0700183 return mDisplayWidth;
184}
185
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700186int DisplayDevice::getHeight() const {
Mathias Agopiana4912602012-07-12 14:25:33 -0700187 return mDisplayHeight;
188}
189
Dominik Laskowskibf170d92018-04-19 15:08:05 -0700190void DisplayDevice::setDisplayName(const std::string& displayName) {
191 if (!displayName.empty()) {
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700192 // never override the name with an empty name
193 mDisplayName = displayName;
194 }
195}
196
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700197uint32_t DisplayDevice::getPageFlipCount() const {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700198 return mPageFlipCount;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800199}
200
Chia-I Wub02087d2017-11-09 10:19:54 -0800201void DisplayDevice::flip() const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800202{
Mathias Agopian875d8e12013-06-07 15:35:48 -0700203 mFlinger->getRenderEngine().checkErrors();
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700204 mPageFlipCount++;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800205}
206
Dan Stoza71433162014-02-04 16:22:36 -0800207status_t DisplayDevice::beginFrame(bool mustRecompose) const {
208 return mDisplaySurface->beginFrame(mustRecompose);
Jesse Hall028dc8f2013-08-20 16:35:32 -0700209}
210
Dan Stoza9e56aa02015-11-02 13:00:03 -0800211status_t DisplayDevice::prepareFrame(HWComposer& hwc) {
212 status_t error = hwc.prepare(*this);
213 if (error != NO_ERROR) {
214 return error;
215 }
216
217 DisplaySurface::CompositionType compositionType;
218 bool hasClient = hwc.hasClientComposition(mHwcDisplayId);
219 bool hasDevice = hwc.hasDeviceComposition(mHwcDisplayId);
220 if (hasClient && hasDevice) {
221 compositionType = DisplaySurface::COMPOSITION_MIXED;
222 } else if (hasClient) {
223 compositionType = DisplaySurface::COMPOSITION_GLES;
224 } else if (hasDevice) {
225 compositionType = DisplaySurface::COMPOSITION_HWC;
226 } else {
227 // Nothing to do -- when turning the screen off we get a frame like
228 // this. Call it a HWC frame since we won't be doing any GLES work but
229 // will do a prepare/set cycle.
230 compositionType = DisplaySurface::COMPOSITION_HWC;
231 }
232 return mDisplaySurface->prepareFrame(compositionType);
233}
Jesse Hall38efe862013-04-06 23:12:29 -0700234
Mathias Agopianda27af92012-09-13 18:17:13 -0700235void DisplayDevice::swapBuffers(HWComposer& hwc) const {
Madhuri Athota88a905b2017-05-04 16:58:15 +0530236 if (hwc.hasClientComposition(mHwcDisplayId) || hwc.hasFlipClientTargetRequest(mHwcDisplayId)) {
Lloyd Pique144e1162017-12-20 16:44:52 -0800237 mSurface->swapBuffers();
Mathias Agopianda27af92012-09-13 18:17:13 -0700238 }
Mathias Agopian52e21482012-09-24 18:07:21 -0700239
Jesse Hall99c7dbb2013-03-14 14:29:29 -0700240 status_t result = mDisplaySurface->advanceFrame();
241 if (result != NO_ERROR) {
Dominik Laskowskibf170d92018-04-19 15:08:05 -0700242 ALOGE("[%s] failed pushing new frame to HWC: %d", mDisplayName.c_str(), result);
Mathias Agopian32341382012-09-25 19:16:28 -0700243 }
Mathias Agopianda27af92012-09-13 18:17:13 -0700244}
245
Dan Stoza9e56aa02015-11-02 13:00:03 -0800246void DisplayDevice::onSwapBuffersCompleted() const {
247 mDisplaySurface->onFrameCommitted();
248}
Mathias Agopianda27af92012-09-13 18:17:13 -0700249
Chia-I Wuf846a352017-11-10 09:22:52 -0800250bool DisplayDevice::makeCurrent() const {
Lloyd Pique144e1162017-12-20 16:44:52 -0800251 bool success = mFlinger->getRenderEngine().setCurrentSurface(*mSurface);
Mathias Agopian931bda12013-08-28 18:11:46 -0700252 setViewportAndProjection();
Chia-I Wuf846a352017-11-10 09:22:52 -0800253 return success;
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700254}
255
Mathias Agopian875d8e12013-06-07 15:35:48 -0700256void DisplayDevice::setViewportAndProjection() const {
257 size_t w = mDisplayWidth;
258 size_t h = mDisplayHeight;
Dan Stozac1879002014-05-22 15:59:05 -0700259 Rect sourceCrop(0, 0, w, h);
Riley Andrewsc3ebe662014-09-04 16:20:31 -0700260 mFlinger->getRenderEngine().setViewportAndProjection(w, h, sourceCrop, h,
261 false, Transform::ROT_0);
Mathias Agopianbae92d02012-09-28 01:00:47 -0700262}
263
Dan Stoza9e56aa02015-11-02 13:00:03 -0800264const sp<Fence>& DisplayDevice::getClientTargetAcquireFence() const {
265 return mDisplaySurface->getClientTargetAcquireFence();
266}
Dan Stoza9e56aa02015-11-02 13:00:03 -0800267
Mathias Agopian1b031492012-06-20 17:51:20 -0700268// ----------------------------------------------------------------------------
269
Mathias Agopian13127d82013-03-05 17:47:11 -0800270void DisplayDevice::setVisibleLayersSortedByZ(const Vector< sp<Layer> >& layers) {
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700271 mVisibleLayersSortedByZ = layers;
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700272}
273
Mathias Agopian13127d82013-03-05 17:47:11 -0800274const Vector< sp<Layer> >& DisplayDevice::getVisibleLayersSortedByZ() const {
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700275 return mVisibleLayersSortedByZ;
276}
277
Chia-I Wu83806892017-11-16 10:50:20 -0800278void DisplayDevice::setLayersNeedingFences(const Vector< sp<Layer> >& layers) {
279 mLayersNeedingFences = layers;
280}
281
282const Vector< sp<Layer> >& DisplayDevice::getLayersNeedingFences() const {
283 return mLayersNeedingFences;
284}
285
Mathias Agopiancd60f992012-08-16 16:28:27 -0700286Region DisplayDevice::getDirtyRegion(bool repaintEverything) const {
287 Region dirty;
Mathias Agopiancd60f992012-08-16 16:28:27 -0700288 if (repaintEverything) {
289 dirty.set(getBounds());
290 } else {
Mathias Agopianda8d0a52012-09-04 15:05:38 -0700291 const Transform& planeTransform(mGlobalTransform);
Mathias Agopiancd60f992012-08-16 16:28:27 -0700292 dirty = planeTransform.transform(this->dirtyRegion);
293 dirty.andSelf(getBounds());
294 }
295 return dirty;
296}
297
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700298// ----------------------------------------------------------------------------
Prashant Malani2c9b11f2014-05-25 01:36:31 -0700299void DisplayDevice::setPowerMode(int mode) {
300 mPowerMode = mode;
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700301}
302
Prashant Malani2c9b11f2014-05-25 01:36:31 -0700303int DisplayDevice::getPowerMode() const {
304 return mPowerMode;
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700305}
306
Prashant Malani2c9b11f2014-05-25 01:36:31 -0700307bool DisplayDevice::isDisplayOn() const {
308 return (mPowerMode != HWC_POWER_MODE_OFF);
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700309}
310
311// ----------------------------------------------------------------------------
Michael Lentine6c9e34a2014-07-14 13:48:55 -0700312void DisplayDevice::setActiveConfig(int mode) {
313 mActiveConfig = mode;
314}
315
316int DisplayDevice::getActiveConfig() const {
317 return mActiveConfig;
318}
319
320// ----------------------------------------------------------------------------
Peiyong Lina52f0292018-03-14 17:26:31 -0700321void DisplayDevice::setActiveColorMode(ColorMode mode) {
Michael Wright28f24d02016-07-12 13:30:53 -0700322 mActiveColorMode = mode;
323}
324
Peiyong Lina52f0292018-03-14 17:26:31 -0700325ColorMode DisplayDevice::getActiveColorMode() const {
Michael Wright28f24d02016-07-12 13:30:53 -0700326 return mActiveColorMode;
327}
Courtney Goeltzenleuchter79d27242017-07-13 17:54:01 -0600328
Peiyong Lindd9b2ae2018-03-01 16:22:45 -0800329RenderIntent DisplayDevice::getActiveRenderIntent() const {
330 return mActiveRenderIntent;
331}
332
333void DisplayDevice::setActiveRenderIntent(RenderIntent renderIntent) {
334 mActiveRenderIntent = renderIntent;
335}
336
Yiwei Zhang7c64f172018-03-07 14:52:28 -0800337void DisplayDevice::setColorTransform(const mat4& transform) {
338 const bool isIdentity = (transform == mat4());
339 mColorTransform =
340 isIdentity ? HAL_COLOR_TRANSFORM_IDENTITY : HAL_COLOR_TRANSFORM_ARBITRARY_MATRIX;
341}
342
343android_color_transform_t DisplayDevice::getColorTransform() const {
344 return mColorTransform;
345}
346
Peiyong Lin34beb7a2018-03-28 11:57:12 -0700347void DisplayDevice::setCompositionDataSpace(ui::Dataspace dataspace) {
Peiyong Lindd9b2ae2018-03-01 16:22:45 -0800348 mCompositionDataSpace = dataspace;
Courtney Goeltzenleuchter79d27242017-07-13 17:54:01 -0600349 ANativeWindow* const window = mNativeWindow.get();
Peiyong Lin34beb7a2018-03-28 11:57:12 -0700350 native_window_set_buffers_data_space(window, static_cast<android_dataspace>(dataspace));
Courtney Goeltzenleuchter79d27242017-07-13 17:54:01 -0600351}
Michael Wright28f24d02016-07-12 13:30:53 -0700352
Peiyong Lindd9b2ae2018-03-01 16:22:45 -0800353ui::Dataspace DisplayDevice::getCompositionDataSpace() const {
354 return mCompositionDataSpace;
355}
356
Michael Wright28f24d02016-07-12 13:30:53 -0700357// ----------------------------------------------------------------------------
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700358
Mathias Agopian28947d72012-08-08 18:51:15 -0700359void DisplayDevice::setLayerStack(uint32_t stack) {
360 mLayerStack = stack;
361 dirtyRegion.set(bounds());
362}
363
364// ----------------------------------------------------------------------------
365
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700366uint32_t DisplayDevice::getOrientationTransform() const {
367 uint32_t transform = 0;
368 switch (mOrientation) {
369 case DisplayState::eOrientationDefault:
370 transform = Transform::ROT_0;
371 break;
372 case DisplayState::eOrientation90:
373 transform = Transform::ROT_90;
374 break;
375 case DisplayState::eOrientation180:
376 transform = Transform::ROT_180;
377 break;
378 case DisplayState::eOrientation270:
379 transform = Transform::ROT_270;
380 break;
381 }
382 return transform;
383}
384
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700385status_t DisplayDevice::orientationToTransfrom(
Mathias Agopian1b031492012-06-20 17:51:20 -0700386 int orientation, int w, int h, Transform* tr)
387{
388 uint32_t flags = 0;
389 switch (orientation) {
Mathias Agopian3165cc22012-08-08 19:42:09 -0700390 case DisplayState::eOrientationDefault:
Mathias Agopian1b031492012-06-20 17:51:20 -0700391 flags = Transform::ROT_0;
392 break;
Mathias Agopian3165cc22012-08-08 19:42:09 -0700393 case DisplayState::eOrientation90:
Mathias Agopian1b031492012-06-20 17:51:20 -0700394 flags = Transform::ROT_90;
395 break;
Mathias Agopian3165cc22012-08-08 19:42:09 -0700396 case DisplayState::eOrientation180:
Mathias Agopian1b031492012-06-20 17:51:20 -0700397 flags = Transform::ROT_180;
398 break;
Mathias Agopian3165cc22012-08-08 19:42:09 -0700399 case DisplayState::eOrientation270:
Mathias Agopian1b031492012-06-20 17:51:20 -0700400 flags = Transform::ROT_270;
401 break;
402 default:
403 return BAD_VALUE;
404 }
405 tr->set(flags, w, h);
406 return NO_ERROR;
407}
408
Michael Lentine47e45402014-07-18 15:34:25 -0700409void DisplayDevice::setDisplaySize(const int newWidth, const int newHeight) {
410 dirtyRegion.set(getBounds());
411
Lloyd Pique144e1162017-12-20 16:44:52 -0800412 mSurface->setNativeWindow(nullptr);
Michael Lentinef2568de2014-08-20 10:51:23 -0700413
Michael Lentine47e45402014-07-18 15:34:25 -0700414 mDisplaySurface->resizeBuffers(newWidth, newHeight);
415
416 ANativeWindow* const window = mNativeWindow.get();
Lloyd Pique144e1162017-12-20 16:44:52 -0800417 mSurface->setNativeWindow(window);
418 mDisplayWidth = mSurface->queryWidth();
419 mDisplayHeight = mSurface->queryHeight();
Michael Lentine47e45402014-07-18 15:34:25 -0700420
421 LOG_FATAL_IF(mDisplayWidth != newWidth,
422 "Unable to set new width to %d", newWidth);
423 LOG_FATAL_IF(mDisplayHeight != newHeight,
424 "Unable to set new height to %d", newHeight);
425}
426
Mathias Agopian00e8c7a2012-09-04 19:30:46 -0700427void DisplayDevice::setProjection(int orientation,
Mathias Agopianf5f714a2013-02-26 16:54:05 -0800428 const Rect& newViewport, const Rect& newFrame) {
429 Rect viewport(newViewport);
430 Rect frame(newFrame);
431
432 const int w = mDisplayWidth;
433 const int h = mDisplayHeight;
434
435 Transform R;
436 DisplayDevice::orientationToTransfrom(orientation, w, h, &R);
437
438 if (!frame.isValid()) {
439 // the destination frame can be invalid if it has never been set,
440 // in that case we assume the whole display frame.
441 frame = Rect(w, h);
442 }
443
444 if (viewport.isEmpty()) {
445 // viewport can be invalid if it has never been set, in that case
446 // we assume the whole display size.
447 // it's also invalid to have an empty viewport, so we handle that
448 // case in the same way.
449 viewport = Rect(w, h);
450 if (R.getOrientation() & Transform::ROT_90) {
451 // viewport is always specified in the logical orientation
452 // of the display (ie: post-rotation).
453 swap(viewport.right, viewport.bottom);
454 }
455 }
456
457 dirtyRegion.set(getBounds());
458
459 Transform TL, TP, S;
460 float src_width = viewport.width();
461 float src_height = viewport.height();
462 float dst_width = frame.width();
463 float dst_height = frame.height();
464 if (src_width != dst_width || src_height != dst_height) {
465 float sx = dst_width / src_width;
466 float sy = dst_height / src_height;
467 S.set(sx, 0, 0, sy);
468 }
469
470 float src_x = viewport.left;
471 float src_y = viewport.top;
472 float dst_x = frame.left;
473 float dst_y = frame.top;
474 TL.set(-src_x, -src_y);
475 TP.set(dst_x, dst_y);
476
477 // The viewport and frame are both in the logical orientation.
478 // Apply the logical translation, scale to physical size, apply the
479 // physical translation and finally rotate to the physical orientation.
480 mGlobalTransform = R * TP * S * TL;
481
482 const uint8_t type = mGlobalTransform.getType();
483 mNeedsFiltering = (!mGlobalTransform.preserveRects() ||
484 (type >= Transform::SCALE));
485
486 mScissor = mGlobalTransform.transform(viewport);
487 if (mScissor.isEmpty()) {
Mathias Agopian6c7f25a2013-05-09 20:37:10 -0700488 mScissor = getBounds();
Mathias Agopianf5f714a2013-02-26 16:54:05 -0800489 }
490
Mathias Agopianda8d0a52012-09-04 15:05:38 -0700491 mOrientation = orientation;
Dominik Laskowski281644e2018-04-19 15:47:35 -0700492 if (isPrimary()) {
Pablo Ceballos021623b2016-04-15 17:31:51 -0700493 uint32_t transform = 0;
494 switch (mOrientation) {
495 case DisplayState::eOrientationDefault:
496 transform = Transform::ROT_0;
497 break;
498 case DisplayState::eOrientation90:
499 transform = Transform::ROT_90;
500 break;
501 case DisplayState::eOrientation180:
502 transform = Transform::ROT_180;
503 break;
504 case DisplayState::eOrientation270:
505 transform = Transform::ROT_270;
506 break;
507 }
508 sPrimaryDisplayOrientation = transform;
509 }
Mathias Agopian00e8c7a2012-09-04 19:30:46 -0700510 mViewport = viewport;
511 mFrame = frame;
Mathias Agopian1b031492012-06-20 17:51:20 -0700512}
Mathias Agopian1d12d8a2012-09-18 01:38:00 -0700513
Pablo Ceballos021623b2016-04-15 17:31:51 -0700514uint32_t DisplayDevice::getPrimaryDisplayOrientationTransform() {
515 return sPrimaryDisplayOrientation;
516}
517
Mathias Agopian74d211a2013-04-22 16:55:35 +0200518void DisplayDevice::dump(String8& result) const {
Mathias Agopian1d12d8a2012-09-18 01:38:00 -0700519 const Transform& tr(mGlobalTransform);
Courtney Goeltzenleuchter152279d2017-08-14 18:18:30 -0600520 ANativeWindow* const window = mNativeWindow.get();
Dominik Laskowskibf170d92018-04-19 15:08:05 -0700521 result.appendFormat("+ DisplayDevice: %s\n", mDisplayName.c_str());
Courtney Goeltzenleuchter0ebaac32017-04-13 12:17:03 -0600522 result.appendFormat(" type=%x, hwcId=%d, layerStack=%u, (%4dx%4d), ANativeWindow=%p "
523 "(%d:%d:%d:%d), orient=%2d (type=%08x), "
524 "flips=%u, isSecure=%d, powerMode=%d, activeConfig=%d, numLayers=%zu\n",
Courtney Goeltzenleuchter152279d2017-08-14 18:18:30 -0600525 mType, mHwcDisplayId, mLayerStack, mDisplayWidth, mDisplayHeight, window,
Lloyd Pique144e1162017-12-20 16:44:52 -0800526 mSurface->queryRedSize(), mSurface->queryGreenSize(),
527 mSurface->queryBlueSize(), mSurface->queryAlphaSize(), mOrientation,
528 tr.getType(), getPageFlipCount(), mIsSecure, mPowerMode, mActiveConfig,
Courtney Goeltzenleuchter0ebaac32017-04-13 12:17:03 -0600529 mVisibleLayersSortedByZ.size());
530 result.appendFormat(" v:[%d,%d,%d,%d], f:[%d,%d,%d,%d], s:[%d,%d,%d,%d],"
531 "transform:[[%0.3f,%0.3f,%0.3f][%0.3f,%0.3f,%0.3f][%0.3f,%0.3f,%0.3f]]\n",
532 mViewport.left, mViewport.top, mViewport.right, mViewport.bottom,
533 mFrame.left, mFrame.top, mFrame.right, mFrame.bottom, mScissor.left,
534 mScissor.top, mScissor.right, mScissor.bottom, tr[0][0], tr[1][0], tr[2][0],
535 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 -0600536 auto const surface = static_cast<Surface*>(window);
Peiyong Lin34beb7a2018-03-28 11:57:12 -0700537 ui::Dataspace dataspace = surface->getBuffersDataSpace();
Peiyong Lindd9b2ae2018-03-01 16:22:45 -0800538 result.appendFormat(" wideColorGamut=%d, hdr10=%d, colorMode=%s, dataspace: %s (%d)\n",
539 mHasWideColorGamut, mHasHdr10,
Chia-I Wu1e043612018-03-01 09:45:09 -0800540 decodeColorMode(mActiveColorMode).c_str(),
Peiyong Lin34beb7a2018-03-28 11:57:12 -0700541 dataspaceDetails(static_cast<android_dataspace>(dataspace)).c_str(), dataspace);
Mathias Agopian1d12d8a2012-09-18 01:38:00 -0700542
Jesse Hall99c7dbb2013-03-14 14:29:29 -0700543 String8 surfaceDump;
Dan Stozaf10c46e2014-11-11 10:32:31 -0800544 mDisplaySurface->dumpAsString(surfaceDump);
Jesse Hall99c7dbb2013-03-14 14:29:29 -0700545 result.append(surfaceDump);
Mathias Agopian1d12d8a2012-09-18 01:38:00 -0700546}
Irvelffc9efc2016-07-27 15:16:37 -0700547
Peiyong Lin136fbbc2018-04-17 15:09:44 -0700548void DisplayDevice::hasToneMapping(const std::vector<RenderIntent>& renderIntents,
549 bool* outColorimetric, bool *outEnhance) {
550 for (auto intent : renderIntents) {
551 switch (intent) {
552 case RenderIntent::TONE_MAP_COLORIMETRIC:
553 *outColorimetric = true;
554 break;
555 case RenderIntent::TONE_MAP_ENHANCE:
556 *outEnhance = true;
557 break;
558 default:
559 break;
560 }
561 }
562}
563
Dominik Laskowski663bd282018-04-19 15:26:54 -0700564std::atomic<int32_t> DisplayDeviceState::sNextSequenceId(1);
Peiyong Linfd997e02018-03-28 15:29:00 -0700565
566} // namespace android