blob: d121a86eada066a88556c80c2f8df8bb2386363d [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
Mathias Agopiana4912602012-07-12 14:25:33 -070052// ----------------------------------------------------------------------------
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080053using namespace android;
Mathias Agopiana4912602012-07-12 14:25:33 -070054// ----------------------------------------------------------------------------
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080055
Jaesoo Lee720a7242017-01-31 15:26:18 +090056// retrieve triple buffer setting from configstore
57using namespace android::hardware::configstore;
58using namespace android::hardware::configstore::V1_0;
59
Fabien Sanglard1971b632017-03-10 14:50:03 -080060static bool useTripleFramebuffer = getInt64< ISurfaceFlingerConfigs,
Fabien Sanglard48eb5652017-07-26 14:55:11 -070061 &ISurfaceFlingerConfigs::maxFrameBufferAcquiredBuffers>(2) >= 3;
Jaesoo Lee720a7242017-01-31 15:26:18 +090062
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080063/*
64 * Initialize the display to the specified values.
65 *
66 */
67
Pablo Ceballos021623b2016-04-15 17:31:51 -070068uint32_t DisplayDevice::sPrimaryDisplayOrientation = 0;
69
Courtney Goeltzenleuchter5d943892017-03-22 13:46:46 -060070// clang-format off
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -070071DisplayDevice::DisplayDevice(
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080072 const sp<SurfaceFlinger>& flinger,
Jamie Gennisdd3cb842012-10-19 18:19:11 -070073 DisplayType type,
Jesse Hallffe1f192013-03-22 15:13:48 -070074 int32_t hwcId,
Jamie Gennisdd3cb842012-10-19 18:19:11 -070075 bool isSecure,
76 const wp<IBinder>& displayToken,
Jesse Hall99c7dbb2013-03-14 14:29:29 -070077 const sp<DisplaySurface>& displaySurface,
Mathias Agopiandb89edc2013-08-02 01:40:18 -070078 const sp<IGraphicBufferProducer>& producer,
Courtney Goeltzenleuchter5d943892017-03-22 13:46:46 -060079 bool supportWideColor)
Jesse Hallb7a05492014-08-14 15:45:06 -070080 : lastCompositionHadVisibleLayers(false),
81 mFlinger(flinger),
Dan Stoza9e56aa02015-11-02 13:00:03 -080082 mType(type),
83 mHwcDisplayId(hwcId),
Chih-Wei Huang27e25622013-01-07 17:33:56 +080084 mDisplayToken(displayToken),
Jesse Hall99c7dbb2013-03-14 14:29:29 -070085 mDisplaySurface(displaySurface),
Chia-I Wuf846a352017-11-10 09:22:52 -080086 mSurface{flinger->getRenderEngine()},
Dan Stoza9e56aa02015-11-02 13:00:03 -080087 mDisplayWidth(),
88 mDisplayHeight(),
Mathias Agopian92a979a2012-08-02 18:32:23 -070089 mPageFlipCount(),
Jamie Gennisdd3cb842012-10-19 18:19:11 -070090 mIsSecure(isSecure),
Jesse Hall01e29052013-02-19 16:13:35 -080091 mLayerStack(NO_LAYER_STACK),
Prashant Malani2c9b11f2014-05-25 01:36:31 -070092 mOrientation(),
Michael Lentine6c9e34a2014-07-14 13:48:55 -070093 mPowerMode(HWC_POWER_MODE_OFF),
Courtney Goeltzenleuchter4f20f9c2017-04-06 08:18:34 -060094 mActiveConfig(0)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080095{
Courtney Goeltzenleuchter5d943892017-03-22 13:46:46 -060096 // clang-format on
james.zhang5e8eb5e2015-12-01 17:55:11 +080097 Surface* surface;
98 mNativeWindow = surface = new Surface(producer, false);
Jesse Hallffe1f192013-03-22 15:13:48 -070099 ANativeWindow* const window = mNativeWindow.get();
100
Naseer Ahmed901a86e2017-05-11 11:15:21 -0400101 mActiveColorMode = HAL_COLOR_MODE_NATIVE;
Courtney Goeltzenleuchter4f20f9c2017-04-06 08:18:34 -0600102 mDisplayHasWideColor = supportWideColor;
Jesse Hallffe1f192013-03-22 15:13:48 -0700103 /*
104 * Create our display's surface
105 */
Chia-I Wuf846a352017-11-10 09:22:52 -0800106 mSurface.setCritical(mType == DisplayDevice::DISPLAY_PRIMARY);
107 mSurface.setAsync(mType >= DisplayDevice::DISPLAY_VIRTUAL);
108 mSurface.setNativeWindow(window);
109 mDisplayWidth = mSurface.queryWidth();
110 mDisplayHeight = mSurface.queryHeight();
Jesse Hallffe1f192013-03-22 15:13:48 -0700111
John Dong4ee56962014-02-21 12:37:59 -0800112 // Make sure that composition can never be stalled by a virtual display
113 // consumer that isn't processing buffers fast enough. We have to do this
114 // in two places:
115 // * Here, in case the display is composed entirely by HWC.
116 // * In makeCurrent(), using eglSwapInterval. Some EGL drivers set the
117 // window's swap interval in eglMakeCurrent, so they'll override the
118 // interval we set here.
119 if (mType >= DisplayDevice::DISPLAY_VIRTUAL)
120 window->setSwapInterval(window, 0);
121
Jesse Hallffe1f192013-03-22 15:13:48 -0700122 mPageFlipCount = 0;
123 mViewport.makeInvalid();
124 mFrame.makeInvalid();
125
126 // virtual displays are always considered enabled
Prashant Malani2c9b11f2014-05-25 01:36:31 -0700127 mPowerMode = (mType >= DisplayDevice::DISPLAY_VIRTUAL) ?
128 HWC_POWER_MODE_NORMAL : HWC_POWER_MODE_OFF;
Jesse Hallffe1f192013-03-22 15:13:48 -0700129
Jesse Hallffe1f192013-03-22 15:13:48 -0700130 // initialize the display orientation transform.
131 setProjection(DisplayState::eOrientationDefault, mViewport, mFrame);
james.zhang5e8eb5e2015-12-01 17:55:11 +0800132
Jaesoo Lee720a7242017-01-31 15:26:18 +0900133 if (useTripleFramebuffer) {
134 surface->allocateBuffers();
135 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800136}
137
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700138DisplayDevice::~DisplayDevice() {
Mathias Agopian92a979a2012-08-02 18:32:23 -0700139}
140
Jesse Hall02d86562013-03-25 14:43:23 -0700141void DisplayDevice::disconnect(HWComposer& hwc) {
142 if (mHwcDisplayId >= 0) {
143 hwc.disconnectDisplay(mHwcDisplayId);
Jesse Hall02d86562013-03-25 14:43:23 -0700144 mHwcDisplayId = -1;
145 }
146}
147
Mathias Agopian92a979a2012-08-02 18:32:23 -0700148bool DisplayDevice::isValid() const {
Peiyong Lin566a3b42018-01-09 18:22:43 -0800149 return mFlinger != nullptr;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800150}
151
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700152int DisplayDevice::getWidth() const {
Mathias Agopiana4912602012-07-12 14:25:33 -0700153 return mDisplayWidth;
154}
155
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700156int DisplayDevice::getHeight() const {
Mathias Agopiana4912602012-07-12 14:25:33 -0700157 return mDisplayHeight;
158}
159
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700160void DisplayDevice::setDisplayName(const String8& displayName) {
161 if (!displayName.isEmpty()) {
162 // never override the name with an empty name
163 mDisplayName = displayName;
164 }
165}
166
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700167uint32_t DisplayDevice::getPageFlipCount() const {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700168 return mPageFlipCount;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800169}
170
Chia-I Wub02087d2017-11-09 10:19:54 -0800171void DisplayDevice::flip() const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800172{
Mathias Agopian875d8e12013-06-07 15:35:48 -0700173 mFlinger->getRenderEngine().checkErrors();
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700174 mPageFlipCount++;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800175}
176
Dan Stoza71433162014-02-04 16:22:36 -0800177status_t DisplayDevice::beginFrame(bool mustRecompose) const {
178 return mDisplaySurface->beginFrame(mustRecompose);
Jesse Hall028dc8f2013-08-20 16:35:32 -0700179}
180
Dan Stoza9e56aa02015-11-02 13:00:03 -0800181status_t DisplayDevice::prepareFrame(HWComposer& hwc) {
182 status_t error = hwc.prepare(*this);
183 if (error != NO_ERROR) {
184 return error;
185 }
186
187 DisplaySurface::CompositionType compositionType;
188 bool hasClient = hwc.hasClientComposition(mHwcDisplayId);
189 bool hasDevice = hwc.hasDeviceComposition(mHwcDisplayId);
190 if (hasClient && hasDevice) {
191 compositionType = DisplaySurface::COMPOSITION_MIXED;
192 } else if (hasClient) {
193 compositionType = DisplaySurface::COMPOSITION_GLES;
194 } else if (hasDevice) {
195 compositionType = DisplaySurface::COMPOSITION_HWC;
196 } else {
197 // Nothing to do -- when turning the screen off we get a frame like
198 // this. Call it a HWC frame since we won't be doing any GLES work but
199 // will do a prepare/set cycle.
200 compositionType = DisplaySurface::COMPOSITION_HWC;
201 }
202 return mDisplaySurface->prepareFrame(compositionType);
203}
Jesse Hall38efe862013-04-06 23:12:29 -0700204
Mathias Agopianda27af92012-09-13 18:17:13 -0700205void DisplayDevice::swapBuffers(HWComposer& hwc) const {
Madhuri Athota88a905b2017-05-04 16:58:15 +0530206 if (hwc.hasClientComposition(mHwcDisplayId) || hwc.hasFlipClientTargetRequest(mHwcDisplayId)) {
Chia-I Wuf846a352017-11-10 09:22:52 -0800207 mSurface.swapBuffers();
Mathias Agopianda27af92012-09-13 18:17:13 -0700208 }
Mathias Agopian52e21482012-09-24 18:07:21 -0700209
Jesse Hall99c7dbb2013-03-14 14:29:29 -0700210 status_t result = mDisplaySurface->advanceFrame();
211 if (result != NO_ERROR) {
212 ALOGE("[%s] failed pushing new frame to HWC: %d",
213 mDisplayName.string(), result);
Mathias Agopian32341382012-09-25 19:16:28 -0700214 }
Mathias Agopianda27af92012-09-13 18:17:13 -0700215}
216
Dan Stoza9e56aa02015-11-02 13:00:03 -0800217void DisplayDevice::onSwapBuffersCompleted() const {
218 mDisplaySurface->onFrameCommitted();
219}
Mathias Agopianda27af92012-09-13 18:17:13 -0700220
Chia-I Wuf846a352017-11-10 09:22:52 -0800221bool DisplayDevice::makeCurrent() const {
222 bool success = mFlinger->getRenderEngine().setCurrentSurface(mSurface);
Mathias Agopian931bda12013-08-28 18:11:46 -0700223 setViewportAndProjection();
Chia-I Wuf846a352017-11-10 09:22:52 -0800224 return success;
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700225}
226
Mathias Agopian875d8e12013-06-07 15:35:48 -0700227void DisplayDevice::setViewportAndProjection() const {
228 size_t w = mDisplayWidth;
229 size_t h = mDisplayHeight;
Dan Stozac1879002014-05-22 15:59:05 -0700230 Rect sourceCrop(0, 0, w, h);
Riley Andrewsc3ebe662014-09-04 16:20:31 -0700231 mFlinger->getRenderEngine().setViewportAndProjection(w, h, sourceCrop, h,
232 false, Transform::ROT_0);
Mathias Agopianbae92d02012-09-28 01:00:47 -0700233}
234
Dan Stoza9e56aa02015-11-02 13:00:03 -0800235const sp<Fence>& DisplayDevice::getClientTargetAcquireFence() const {
236 return mDisplaySurface->getClientTargetAcquireFence();
237}
Dan Stoza9e56aa02015-11-02 13:00:03 -0800238
Mathias Agopian1b031492012-06-20 17:51:20 -0700239// ----------------------------------------------------------------------------
240
Mathias Agopian13127d82013-03-05 17:47:11 -0800241void DisplayDevice::setVisibleLayersSortedByZ(const Vector< sp<Layer> >& layers) {
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700242 mVisibleLayersSortedByZ = layers;
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700243}
244
Mathias Agopian13127d82013-03-05 17:47:11 -0800245const Vector< sp<Layer> >& DisplayDevice::getVisibleLayersSortedByZ() const {
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700246 return mVisibleLayersSortedByZ;
247}
248
Chia-I Wu83806892017-11-16 10:50:20 -0800249void DisplayDevice::setLayersNeedingFences(const Vector< sp<Layer> >& layers) {
250 mLayersNeedingFences = layers;
251}
252
253const Vector< sp<Layer> >& DisplayDevice::getLayersNeedingFences() const {
254 return mLayersNeedingFences;
255}
256
Mathias Agopiancd60f992012-08-16 16:28:27 -0700257Region DisplayDevice::getDirtyRegion(bool repaintEverything) const {
258 Region dirty;
Mathias Agopiancd60f992012-08-16 16:28:27 -0700259 if (repaintEverything) {
260 dirty.set(getBounds());
261 } else {
Mathias Agopianda8d0a52012-09-04 15:05:38 -0700262 const Transform& planeTransform(mGlobalTransform);
Mathias Agopiancd60f992012-08-16 16:28:27 -0700263 dirty = planeTransform.transform(this->dirtyRegion);
264 dirty.andSelf(getBounds());
265 }
266 return dirty;
267}
268
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700269// ----------------------------------------------------------------------------
Prashant Malani2c9b11f2014-05-25 01:36:31 -0700270void DisplayDevice::setPowerMode(int mode) {
271 mPowerMode = mode;
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700272}
273
Prashant Malani2c9b11f2014-05-25 01:36:31 -0700274int DisplayDevice::getPowerMode() const {
275 return mPowerMode;
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700276}
277
Prashant Malani2c9b11f2014-05-25 01:36:31 -0700278bool DisplayDevice::isDisplayOn() const {
279 return (mPowerMode != HWC_POWER_MODE_OFF);
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700280}
281
282// ----------------------------------------------------------------------------
Michael Lentine6c9e34a2014-07-14 13:48:55 -0700283void DisplayDevice::setActiveConfig(int mode) {
284 mActiveConfig = mode;
285}
286
287int DisplayDevice::getActiveConfig() const {
288 return mActiveConfig;
289}
290
291// ----------------------------------------------------------------------------
Michael Wright28f24d02016-07-12 13:30:53 -0700292void DisplayDevice::setActiveColorMode(android_color_mode_t mode) {
293 mActiveColorMode = mode;
294}
295
296android_color_mode_t DisplayDevice::getActiveColorMode() const {
297 return mActiveColorMode;
298}
Courtney Goeltzenleuchter79d27242017-07-13 17:54:01 -0600299
300void DisplayDevice::setCompositionDataSpace(android_dataspace dataspace) {
301 ANativeWindow* const window = mNativeWindow.get();
302 native_window_set_buffers_data_space(window, dataspace);
303}
Michael Wright28f24d02016-07-12 13:30:53 -0700304
305// ----------------------------------------------------------------------------
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700306
Mathias Agopian28947d72012-08-08 18:51:15 -0700307void DisplayDevice::setLayerStack(uint32_t stack) {
308 mLayerStack = stack;
309 dirtyRegion.set(bounds());
310}
311
312// ----------------------------------------------------------------------------
313
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700314uint32_t DisplayDevice::getOrientationTransform() const {
315 uint32_t transform = 0;
316 switch (mOrientation) {
317 case DisplayState::eOrientationDefault:
318 transform = Transform::ROT_0;
319 break;
320 case DisplayState::eOrientation90:
321 transform = Transform::ROT_90;
322 break;
323 case DisplayState::eOrientation180:
324 transform = Transform::ROT_180;
325 break;
326 case DisplayState::eOrientation270:
327 transform = Transform::ROT_270;
328 break;
329 }
330 return transform;
331}
332
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700333status_t DisplayDevice::orientationToTransfrom(
Mathias Agopian1b031492012-06-20 17:51:20 -0700334 int orientation, int w, int h, Transform* tr)
335{
336 uint32_t flags = 0;
337 switch (orientation) {
Mathias Agopian3165cc22012-08-08 19:42:09 -0700338 case DisplayState::eOrientationDefault:
Mathias Agopian1b031492012-06-20 17:51:20 -0700339 flags = Transform::ROT_0;
340 break;
Mathias Agopian3165cc22012-08-08 19:42:09 -0700341 case DisplayState::eOrientation90:
Mathias Agopian1b031492012-06-20 17:51:20 -0700342 flags = Transform::ROT_90;
343 break;
Mathias Agopian3165cc22012-08-08 19:42:09 -0700344 case DisplayState::eOrientation180:
Mathias Agopian1b031492012-06-20 17:51:20 -0700345 flags = Transform::ROT_180;
346 break;
Mathias Agopian3165cc22012-08-08 19:42:09 -0700347 case DisplayState::eOrientation270:
Mathias Agopian1b031492012-06-20 17:51:20 -0700348 flags = Transform::ROT_270;
349 break;
350 default:
351 return BAD_VALUE;
352 }
353 tr->set(flags, w, h);
354 return NO_ERROR;
355}
356
Michael Lentine47e45402014-07-18 15:34:25 -0700357void DisplayDevice::setDisplaySize(const int newWidth, const int newHeight) {
358 dirtyRegion.set(getBounds());
359
Chia-I Wuf846a352017-11-10 09:22:52 -0800360 mSurface.setNativeWindow(nullptr);
Michael Lentinef2568de2014-08-20 10:51:23 -0700361
Michael Lentine47e45402014-07-18 15:34:25 -0700362 mDisplaySurface->resizeBuffers(newWidth, newHeight);
363
364 ANativeWindow* const window = mNativeWindow.get();
Chia-I Wuf846a352017-11-10 09:22:52 -0800365 mSurface.setNativeWindow(window);
366 mDisplayWidth = mSurface.queryWidth();
367 mDisplayHeight = mSurface.queryHeight();
Michael Lentine47e45402014-07-18 15:34:25 -0700368
369 LOG_FATAL_IF(mDisplayWidth != newWidth,
370 "Unable to set new width to %d", newWidth);
371 LOG_FATAL_IF(mDisplayHeight != newHeight,
372 "Unable to set new height to %d", newHeight);
373}
374
Mathias Agopian00e8c7a2012-09-04 19:30:46 -0700375void DisplayDevice::setProjection(int orientation,
Mathias Agopianf5f714a2013-02-26 16:54:05 -0800376 const Rect& newViewport, const Rect& newFrame) {
377 Rect viewport(newViewport);
378 Rect frame(newFrame);
379
380 const int w = mDisplayWidth;
381 const int h = mDisplayHeight;
382
383 Transform R;
384 DisplayDevice::orientationToTransfrom(orientation, w, h, &R);
385
386 if (!frame.isValid()) {
387 // the destination frame can be invalid if it has never been set,
388 // in that case we assume the whole display frame.
389 frame = Rect(w, h);
390 }
391
392 if (viewport.isEmpty()) {
393 // viewport can be invalid if it has never been set, in that case
394 // we assume the whole display size.
395 // it's also invalid to have an empty viewport, so we handle that
396 // case in the same way.
397 viewport = Rect(w, h);
398 if (R.getOrientation() & Transform::ROT_90) {
399 // viewport is always specified in the logical orientation
400 // of the display (ie: post-rotation).
401 swap(viewport.right, viewport.bottom);
402 }
403 }
404
405 dirtyRegion.set(getBounds());
406
407 Transform TL, TP, S;
408 float src_width = viewport.width();
409 float src_height = viewport.height();
410 float dst_width = frame.width();
411 float dst_height = frame.height();
412 if (src_width != dst_width || src_height != dst_height) {
413 float sx = dst_width / src_width;
414 float sy = dst_height / src_height;
415 S.set(sx, 0, 0, sy);
416 }
417
418 float src_x = viewport.left;
419 float src_y = viewport.top;
420 float dst_x = frame.left;
421 float dst_y = frame.top;
422 TL.set(-src_x, -src_y);
423 TP.set(dst_x, dst_y);
424
425 // The viewport and frame are both in the logical orientation.
426 // Apply the logical translation, scale to physical size, apply the
427 // physical translation and finally rotate to the physical orientation.
428 mGlobalTransform = R * TP * S * TL;
429
430 const uint8_t type = mGlobalTransform.getType();
431 mNeedsFiltering = (!mGlobalTransform.preserveRects() ||
432 (type >= Transform::SCALE));
433
434 mScissor = mGlobalTransform.transform(viewport);
435 if (mScissor.isEmpty()) {
Mathias Agopian6c7f25a2013-05-09 20:37:10 -0700436 mScissor = getBounds();
Mathias Agopianf5f714a2013-02-26 16:54:05 -0800437 }
438
Mathias Agopianda8d0a52012-09-04 15:05:38 -0700439 mOrientation = orientation;
Pablo Ceballos021623b2016-04-15 17:31:51 -0700440 if (mType == DisplayType::DISPLAY_PRIMARY) {
441 uint32_t transform = 0;
442 switch (mOrientation) {
443 case DisplayState::eOrientationDefault:
444 transform = Transform::ROT_0;
445 break;
446 case DisplayState::eOrientation90:
447 transform = Transform::ROT_90;
448 break;
449 case DisplayState::eOrientation180:
450 transform = Transform::ROT_180;
451 break;
452 case DisplayState::eOrientation270:
453 transform = Transform::ROT_270;
454 break;
455 }
456 sPrimaryDisplayOrientation = transform;
457 }
Mathias Agopian00e8c7a2012-09-04 19:30:46 -0700458 mViewport = viewport;
459 mFrame = frame;
Mathias Agopian1b031492012-06-20 17:51:20 -0700460}
Mathias Agopian1d12d8a2012-09-18 01:38:00 -0700461
Pablo Ceballos021623b2016-04-15 17:31:51 -0700462uint32_t DisplayDevice::getPrimaryDisplayOrientationTransform() {
463 return sPrimaryDisplayOrientation;
464}
465
Mathias Agopian74d211a2013-04-22 16:55:35 +0200466void DisplayDevice::dump(String8& result) const {
Mathias Agopian1d12d8a2012-09-18 01:38:00 -0700467 const Transform& tr(mGlobalTransform);
Courtney Goeltzenleuchter152279d2017-08-14 18:18:30 -0600468 ANativeWindow* const window = mNativeWindow.get();
Courtney Goeltzenleuchter0ebaac32017-04-13 12:17:03 -0600469 result.appendFormat("+ DisplayDevice: %s\n", mDisplayName.string());
470 result.appendFormat(" type=%x, hwcId=%d, layerStack=%u, (%4dx%4d), ANativeWindow=%p "
471 "(%d:%d:%d:%d), orient=%2d (type=%08x), "
472 "flips=%u, isSecure=%d, powerMode=%d, activeConfig=%d, numLayers=%zu\n",
Courtney Goeltzenleuchter152279d2017-08-14 18:18:30 -0600473 mType, mHwcDisplayId, mLayerStack, mDisplayWidth, mDisplayHeight, window,
Chia-I Wuf846a352017-11-10 09:22:52 -0800474 mSurface.queryRedSize(), mSurface.queryGreenSize(), mSurface.queryBlueSize(),
475 mSurface.queryAlphaSize(), mOrientation, tr.getType(),
Courtney Goeltzenleuchter152279d2017-08-14 18:18:30 -0600476 getPageFlipCount(), mIsSecure, mPowerMode, mActiveConfig,
Courtney Goeltzenleuchter0ebaac32017-04-13 12:17:03 -0600477 mVisibleLayersSortedByZ.size());
478 result.appendFormat(" v:[%d,%d,%d,%d], f:[%d,%d,%d,%d], s:[%d,%d,%d,%d],"
479 "transform:[[%0.3f,%0.3f,%0.3f][%0.3f,%0.3f,%0.3f][%0.3f,%0.3f,%0.3f]]\n",
480 mViewport.left, mViewport.top, mViewport.right, mViewport.bottom,
481 mFrame.left, mFrame.top, mFrame.right, mFrame.bottom, mScissor.left,
482 mScissor.top, mScissor.right, mScissor.bottom, tr[0][0], tr[1][0], tr[2][0],
483 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 -0600484 auto const surface = static_cast<Surface*>(window);
485 android_dataspace dataspace = surface->getBuffersDataSpace();
486 result.appendFormat(" dataspace: %s (%d)\n", dataspaceDetails(dataspace).c_str(), dataspace);
Mathias Agopian1d12d8a2012-09-18 01:38:00 -0700487
Jesse Hall99c7dbb2013-03-14 14:29:29 -0700488 String8 surfaceDump;
Dan Stozaf10c46e2014-11-11 10:32:31 -0800489 mDisplaySurface->dumpAsString(surfaceDump);
Jesse Hall99c7dbb2013-03-14 14:29:29 -0700490 result.append(surfaceDump);
Mathias Agopian1d12d8a2012-09-18 01:38:00 -0700491}
Irvelffc9efc2016-07-27 15:16:37 -0700492
493std::atomic<int32_t> DisplayDeviceState::nextDisplayId(1);
494
495DisplayDeviceState::DisplayDeviceState(DisplayDevice::DisplayType type, bool isSecure)
496 : type(type),
497 layerStack(DisplayDevice::NO_LAYER_STACK),
498 orientation(0),
499 width(0),
500 height(0),
501 isSecure(isSecure)
502{
503 viewport.makeInvalid();
504 frame.makeInvalid();
505}