blob: 2b1e5775bad8d0c4907238e53dfd26d97fc0a3ee [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,
Peiyong Lind6fa4a72018-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 Lin2c327ac2018-04-19 22:06:34 -0700108 mHasDolbyVision(false),
Peiyong Lind6fa4a72018-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 Lind6fa4a72018-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
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700190void DisplayDevice::setDisplayName(const String8& displayName) {
191 if (!displayName.isEmpty()) {
192 // 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) {
242 ALOGE("[%s] failed pushing new frame to HWC: %d",
243 mDisplayName.string(), result);
Mathias Agopian32341382012-09-25 19:16:28 -0700244 }
Mathias Agopianda27af92012-09-13 18:17:13 -0700245}
246
Dan Stoza9e56aa02015-11-02 13:00:03 -0800247void DisplayDevice::onSwapBuffersCompleted() const {
248 mDisplaySurface->onFrameCommitted();
249}
Mathias Agopianda27af92012-09-13 18:17:13 -0700250
Chia-I Wuf846a352017-11-10 09:22:52 -0800251bool DisplayDevice::makeCurrent() const {
Lloyd Pique144e1162017-12-20 16:44:52 -0800252 bool success = mFlinger->getRenderEngine().setCurrentSurface(*mSurface);
Mathias Agopian931bda12013-08-28 18:11:46 -0700253 setViewportAndProjection();
Chia-I Wuf846a352017-11-10 09:22:52 -0800254 return success;
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700255}
256
Mathias Agopian875d8e12013-06-07 15:35:48 -0700257void DisplayDevice::setViewportAndProjection() const {
258 size_t w = mDisplayWidth;
259 size_t h = mDisplayHeight;
Dan Stozac1879002014-05-22 15:59:05 -0700260 Rect sourceCrop(0, 0, w, h);
Riley Andrewsc3ebe662014-09-04 16:20:31 -0700261 mFlinger->getRenderEngine().setViewportAndProjection(w, h, sourceCrop, h,
262 false, Transform::ROT_0);
Mathias Agopianbae92d02012-09-28 01:00:47 -0700263}
264
Dan Stoza9e56aa02015-11-02 13:00:03 -0800265const sp<Fence>& DisplayDevice::getClientTargetAcquireFence() const {
266 return mDisplaySurface->getClientTargetAcquireFence();
267}
Dan Stoza9e56aa02015-11-02 13:00:03 -0800268
Mathias Agopian1b031492012-06-20 17:51:20 -0700269// ----------------------------------------------------------------------------
270
Mathias Agopian13127d82013-03-05 17:47:11 -0800271void DisplayDevice::setVisibleLayersSortedByZ(const Vector< sp<Layer> >& layers) {
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700272 mVisibleLayersSortedByZ = layers;
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700273}
274
Mathias Agopian13127d82013-03-05 17:47:11 -0800275const Vector< sp<Layer> >& DisplayDevice::getVisibleLayersSortedByZ() const {
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700276 return mVisibleLayersSortedByZ;
277}
278
Chia-I Wu83806892017-11-16 10:50:20 -0800279void DisplayDevice::setLayersNeedingFences(const Vector< sp<Layer> >& layers) {
280 mLayersNeedingFences = layers;
281}
282
283const Vector< sp<Layer> >& DisplayDevice::getLayersNeedingFences() const {
284 return mLayersNeedingFences;
285}
286
Mathias Agopiancd60f992012-08-16 16:28:27 -0700287Region DisplayDevice::getDirtyRegion(bool repaintEverything) const {
288 Region dirty;
Mathias Agopiancd60f992012-08-16 16:28:27 -0700289 if (repaintEverything) {
290 dirty.set(getBounds());
291 } else {
Mathias Agopianda8d0a52012-09-04 15:05:38 -0700292 const Transform& planeTransform(mGlobalTransform);
Mathias Agopiancd60f992012-08-16 16:28:27 -0700293 dirty = planeTransform.transform(this->dirtyRegion);
294 dirty.andSelf(getBounds());
295 }
296 return dirty;
297}
298
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700299// ----------------------------------------------------------------------------
Prashant Malani2c9b11f2014-05-25 01:36:31 -0700300void DisplayDevice::setPowerMode(int mode) {
301 mPowerMode = mode;
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700302}
303
Prashant Malani2c9b11f2014-05-25 01:36:31 -0700304int DisplayDevice::getPowerMode() const {
305 return mPowerMode;
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700306}
307
Prashant Malani2c9b11f2014-05-25 01:36:31 -0700308bool DisplayDevice::isDisplayOn() const {
309 return (mPowerMode != HWC_POWER_MODE_OFF);
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700310}
311
312// ----------------------------------------------------------------------------
Michael Lentine6c9e34a2014-07-14 13:48:55 -0700313void DisplayDevice::setActiveConfig(int mode) {
314 mActiveConfig = mode;
315}
316
317int DisplayDevice::getActiveConfig() const {
318 return mActiveConfig;
319}
320
321// ----------------------------------------------------------------------------
Peiyong Lina52f0292018-03-14 17:26:31 -0700322void DisplayDevice::setActiveColorMode(ColorMode mode) {
Michael Wright28f24d02016-07-12 13:30:53 -0700323 mActiveColorMode = mode;
324}
325
Peiyong Lina52f0292018-03-14 17:26:31 -0700326ColorMode DisplayDevice::getActiveColorMode() const {
Michael Wright28f24d02016-07-12 13:30:53 -0700327 return mActiveColorMode;
328}
Courtney Goeltzenleuchter79d27242017-07-13 17:54:01 -0600329
Peiyong Lindd9b2ae2018-03-01 16:22:45 -0800330RenderIntent DisplayDevice::getActiveRenderIntent() const {
331 return mActiveRenderIntent;
332}
333
334void DisplayDevice::setActiveRenderIntent(RenderIntent renderIntent) {
335 mActiveRenderIntent = renderIntent;
336}
337
Yiwei Zhang7c64f172018-03-07 14:52:28 -0800338void DisplayDevice::setColorTransform(const mat4& transform) {
339 const bool isIdentity = (transform == mat4());
340 mColorTransform =
341 isIdentity ? HAL_COLOR_TRANSFORM_IDENTITY : HAL_COLOR_TRANSFORM_ARBITRARY_MATRIX;
342}
343
344android_color_transform_t DisplayDevice::getColorTransform() const {
345 return mColorTransform;
346}
347
Peiyong Lin34beb7a2018-03-28 11:57:12 -0700348void DisplayDevice::setCompositionDataSpace(ui::Dataspace dataspace) {
Peiyong Lindd9b2ae2018-03-01 16:22:45 -0800349 mCompositionDataSpace = dataspace;
Courtney Goeltzenleuchter79d27242017-07-13 17:54:01 -0600350 ANativeWindow* const window = mNativeWindow.get();
Peiyong Lin34beb7a2018-03-28 11:57:12 -0700351 native_window_set_buffers_data_space(window, static_cast<android_dataspace>(dataspace));
Courtney Goeltzenleuchter79d27242017-07-13 17:54:01 -0600352}
Michael Wright28f24d02016-07-12 13:30:53 -0700353
Peiyong Lindd9b2ae2018-03-01 16:22:45 -0800354ui::Dataspace DisplayDevice::getCompositionDataSpace() const {
355 return mCompositionDataSpace;
356}
357
Michael Wright28f24d02016-07-12 13:30:53 -0700358// ----------------------------------------------------------------------------
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700359
Mathias Agopian28947d72012-08-08 18:51:15 -0700360void DisplayDevice::setLayerStack(uint32_t stack) {
361 mLayerStack = stack;
362 dirtyRegion.set(bounds());
363}
364
365// ----------------------------------------------------------------------------
366
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700367uint32_t DisplayDevice::getOrientationTransform() const {
368 uint32_t transform = 0;
369 switch (mOrientation) {
370 case DisplayState::eOrientationDefault:
371 transform = Transform::ROT_0;
372 break;
373 case DisplayState::eOrientation90:
374 transform = Transform::ROT_90;
375 break;
376 case DisplayState::eOrientation180:
377 transform = Transform::ROT_180;
378 break;
379 case DisplayState::eOrientation270:
380 transform = Transform::ROT_270;
381 break;
382 }
383 return transform;
384}
385
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700386status_t DisplayDevice::orientationToTransfrom(
Mathias Agopian1b031492012-06-20 17:51:20 -0700387 int orientation, int w, int h, Transform* tr)
388{
389 uint32_t flags = 0;
390 switch (orientation) {
Mathias Agopian3165cc22012-08-08 19:42:09 -0700391 case DisplayState::eOrientationDefault:
Mathias Agopian1b031492012-06-20 17:51:20 -0700392 flags = Transform::ROT_0;
393 break;
Mathias Agopian3165cc22012-08-08 19:42:09 -0700394 case DisplayState::eOrientation90:
Mathias Agopian1b031492012-06-20 17:51:20 -0700395 flags = Transform::ROT_90;
396 break;
Mathias Agopian3165cc22012-08-08 19:42:09 -0700397 case DisplayState::eOrientation180:
Mathias Agopian1b031492012-06-20 17:51:20 -0700398 flags = Transform::ROT_180;
399 break;
Mathias Agopian3165cc22012-08-08 19:42:09 -0700400 case DisplayState::eOrientation270:
Mathias Agopian1b031492012-06-20 17:51:20 -0700401 flags = Transform::ROT_270;
402 break;
403 default:
404 return BAD_VALUE;
405 }
406 tr->set(flags, w, h);
407 return NO_ERROR;
408}
409
Michael Lentine47e45402014-07-18 15:34:25 -0700410void DisplayDevice::setDisplaySize(const int newWidth, const int newHeight) {
411 dirtyRegion.set(getBounds());
412
Lloyd Pique144e1162017-12-20 16:44:52 -0800413 mSurface->setNativeWindow(nullptr);
Michael Lentinef2568de2014-08-20 10:51:23 -0700414
Michael Lentine47e45402014-07-18 15:34:25 -0700415 mDisplaySurface->resizeBuffers(newWidth, newHeight);
416
417 ANativeWindow* const window = mNativeWindow.get();
Lloyd Pique144e1162017-12-20 16:44:52 -0800418 mSurface->setNativeWindow(window);
419 mDisplayWidth = mSurface->queryWidth();
420 mDisplayHeight = mSurface->queryHeight();
Michael Lentine47e45402014-07-18 15:34:25 -0700421
422 LOG_FATAL_IF(mDisplayWidth != newWidth,
423 "Unable to set new width to %d", newWidth);
424 LOG_FATAL_IF(mDisplayHeight != newHeight,
425 "Unable to set new height to %d", newHeight);
426}
427
Mathias Agopian00e8c7a2012-09-04 19:30:46 -0700428void DisplayDevice::setProjection(int orientation,
Mathias Agopianf5f714a2013-02-26 16:54:05 -0800429 const Rect& newViewport, const Rect& newFrame) {
430 Rect viewport(newViewport);
431 Rect frame(newFrame);
432
433 const int w = mDisplayWidth;
434 const int h = mDisplayHeight;
435
436 Transform R;
437 DisplayDevice::orientationToTransfrom(orientation, w, h, &R);
438
439 if (!frame.isValid()) {
440 // the destination frame can be invalid if it has never been set,
441 // in that case we assume the whole display frame.
442 frame = Rect(w, h);
443 }
444
445 if (viewport.isEmpty()) {
446 // viewport can be invalid if it has never been set, in that case
447 // we assume the whole display size.
448 // it's also invalid to have an empty viewport, so we handle that
449 // case in the same way.
450 viewport = Rect(w, h);
451 if (R.getOrientation() & Transform::ROT_90) {
452 // viewport is always specified in the logical orientation
453 // of the display (ie: post-rotation).
454 swap(viewport.right, viewport.bottom);
455 }
456 }
457
458 dirtyRegion.set(getBounds());
459
460 Transform TL, TP, S;
461 float src_width = viewport.width();
462 float src_height = viewport.height();
463 float dst_width = frame.width();
464 float dst_height = frame.height();
465 if (src_width != dst_width || src_height != dst_height) {
466 float sx = dst_width / src_width;
467 float sy = dst_height / src_height;
468 S.set(sx, 0, 0, sy);
469 }
470
471 float src_x = viewport.left;
472 float src_y = viewport.top;
473 float dst_x = frame.left;
474 float dst_y = frame.top;
475 TL.set(-src_x, -src_y);
476 TP.set(dst_x, dst_y);
477
478 // The viewport and frame are both in the logical orientation.
479 // Apply the logical translation, scale to physical size, apply the
480 // physical translation and finally rotate to the physical orientation.
481 mGlobalTransform = R * TP * S * TL;
482
483 const uint8_t type = mGlobalTransform.getType();
484 mNeedsFiltering = (!mGlobalTransform.preserveRects() ||
485 (type >= Transform::SCALE));
486
487 mScissor = mGlobalTransform.transform(viewport);
488 if (mScissor.isEmpty()) {
Mathias Agopian6c7f25a2013-05-09 20:37:10 -0700489 mScissor = getBounds();
Mathias Agopianf5f714a2013-02-26 16:54:05 -0800490 }
491
Mathias Agopianda8d0a52012-09-04 15:05:38 -0700492 mOrientation = orientation;
Pablo Ceballos021623b2016-04-15 17:31:51 -0700493 if (mType == DisplayType::DISPLAY_PRIMARY) {
494 uint32_t transform = 0;
495 switch (mOrientation) {
496 case DisplayState::eOrientationDefault:
497 transform = Transform::ROT_0;
498 break;
499 case DisplayState::eOrientation90:
500 transform = Transform::ROT_90;
501 break;
502 case DisplayState::eOrientation180:
503 transform = Transform::ROT_180;
504 break;
505 case DisplayState::eOrientation270:
506 transform = Transform::ROT_270;
507 break;
508 }
509 sPrimaryDisplayOrientation = transform;
510 }
Mathias Agopian00e8c7a2012-09-04 19:30:46 -0700511 mViewport = viewport;
512 mFrame = frame;
Mathias Agopian1b031492012-06-20 17:51:20 -0700513}
Mathias Agopian1d12d8a2012-09-18 01:38:00 -0700514
Pablo Ceballos021623b2016-04-15 17:31:51 -0700515uint32_t DisplayDevice::getPrimaryDisplayOrientationTransform() {
516 return sPrimaryDisplayOrientation;
517}
518
Mathias Agopian74d211a2013-04-22 16:55:35 +0200519void DisplayDevice::dump(String8& result) const {
Mathias Agopian1d12d8a2012-09-18 01:38:00 -0700520 const Transform& tr(mGlobalTransform);
Courtney Goeltzenleuchter152279d2017-08-14 18:18:30 -0600521 ANativeWindow* const window = mNativeWindow.get();
Courtney Goeltzenleuchter0ebaac32017-04-13 12:17:03 -0600522 result.appendFormat("+ DisplayDevice: %s\n", mDisplayName.string());
523 result.appendFormat(" type=%x, hwcId=%d, layerStack=%u, (%4dx%4d), ANativeWindow=%p "
524 "(%d:%d:%d:%d), orient=%2d (type=%08x), "
525 "flips=%u, isSecure=%d, powerMode=%d, activeConfig=%d, numLayers=%zu\n",
Courtney Goeltzenleuchter152279d2017-08-14 18:18:30 -0600526 mType, mHwcDisplayId, mLayerStack, mDisplayWidth, mDisplayHeight, window,
Lloyd Pique144e1162017-12-20 16:44:52 -0800527 mSurface->queryRedSize(), mSurface->queryGreenSize(),
528 mSurface->queryBlueSize(), mSurface->queryAlphaSize(), mOrientation,
529 tr.getType(), getPageFlipCount(), mIsSecure, mPowerMode, mActiveConfig,
Courtney Goeltzenleuchter0ebaac32017-04-13 12:17:03 -0600530 mVisibleLayersSortedByZ.size());
531 result.appendFormat(" v:[%d,%d,%d,%d], f:[%d,%d,%d,%d], s:[%d,%d,%d,%d],"
532 "transform:[[%0.3f,%0.3f,%0.3f][%0.3f,%0.3f,%0.3f][%0.3f,%0.3f,%0.3f]]\n",
533 mViewport.left, mViewport.top, mViewport.right, mViewport.bottom,
534 mFrame.left, mFrame.top, mFrame.right, mFrame.bottom, mScissor.left,
535 mScissor.top, mScissor.right, mScissor.bottom, tr[0][0], tr[1][0], tr[2][0],
536 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 -0600537 auto const surface = static_cast<Surface*>(window);
Peiyong Lin34beb7a2018-03-28 11:57:12 -0700538 ui::Dataspace dataspace = surface->getBuffersDataSpace();
Peiyong Lindd9b2ae2018-03-01 16:22:45 -0800539 result.appendFormat(" wideColorGamut=%d, hdr10=%d, colorMode=%s, dataspace: %s (%d)\n",
540 mHasWideColorGamut, mHasHdr10,
Chia-I Wu1e043612018-03-01 09:45:09 -0800541 decodeColorMode(mActiveColorMode).c_str(),
Peiyong Lin34beb7a2018-03-28 11:57:12 -0700542 dataspaceDetails(static_cast<android_dataspace>(dataspace)).c_str(), dataspace);
Mathias Agopian1d12d8a2012-09-18 01:38:00 -0700543
Jesse Hall99c7dbb2013-03-14 14:29:29 -0700544 String8 surfaceDump;
Dan Stozaf10c46e2014-11-11 10:32:31 -0800545 mDisplaySurface->dumpAsString(surfaceDump);
Jesse Hall99c7dbb2013-03-14 14:29:29 -0700546 result.append(surfaceDump);
Mathias Agopian1d12d8a2012-09-18 01:38:00 -0700547}
Irvelffc9efc2016-07-27 15:16:37 -0700548
Peiyong Lind6fa4a72018-04-17 15:09:44 -0700549void DisplayDevice::hasToneMapping(const std::vector<RenderIntent>& renderIntents,
550 bool* outColorimetric, bool *outEnhance) {
551 for (auto intent : renderIntents) {
552 switch (intent) {
553 case RenderIntent::TONE_MAP_COLORIMETRIC:
554 *outColorimetric = true;
555 break;
556 case RenderIntent::TONE_MAP_ENHANCE:
557 *outEnhance = true;
558 break;
559 default:
560 break;
561 }
562 }
563}
564
Irvelffc9efc2016-07-27 15:16:37 -0700565std::atomic<int32_t> DisplayDeviceState::nextDisplayId(1);
566
567DisplayDeviceState::DisplayDeviceState(DisplayDevice::DisplayType type, bool isSecure)
568 : type(type),
569 layerStack(DisplayDevice::NO_LAYER_STACK),
570 orientation(0),
571 width(0),
572 height(0),
573 isSecure(isSecure)
574{
575 viewport.makeInvalid();
576 frame.makeInvalid();
577}
Peiyong Linfd997e02018-03-28 15:29:00 -0700578
579} // namespace android