blob: 2753f1109799d8a095e0a888e32c43659ef707a5 [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
130 // Name the display. The name will be replaced shortly if the display
131 // was created with createDisplay().
132 switch (mType) {
133 case DISPLAY_PRIMARY:
134 mDisplayName = "Built-in Screen";
135 break;
136 case DISPLAY_EXTERNAL:
137 mDisplayName = "HDMI Screen";
138 break;
139 default:
140 mDisplayName = "Virtual Screen"; // e.g. Overlay #n
141 break;
142 }
143
144 // initialize the display orientation transform.
145 setProjection(DisplayState::eOrientationDefault, mViewport, mFrame);
james.zhang5e8eb5e2015-12-01 17:55:11 +0800146
Jaesoo Lee720a7242017-01-31 15:26:18 +0900147 if (useTripleFramebuffer) {
148 surface->allocateBuffers();
149 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800150}
151
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700152DisplayDevice::~DisplayDevice() {
Mathias Agopian92a979a2012-08-02 18:32:23 -0700153}
154
Jesse Hall02d86562013-03-25 14:43:23 -0700155void DisplayDevice::disconnect(HWComposer& hwc) {
156 if (mHwcDisplayId >= 0) {
157 hwc.disconnectDisplay(mHwcDisplayId);
Jesse Hall02d86562013-03-25 14:43:23 -0700158 mHwcDisplayId = -1;
159 }
160}
161
Mathias Agopian92a979a2012-08-02 18:32:23 -0700162bool DisplayDevice::isValid() const {
163 return mFlinger != NULL;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800164}
165
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700166int DisplayDevice::getWidth() const {
Mathias Agopiana4912602012-07-12 14:25:33 -0700167 return mDisplayWidth;
168}
169
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700170int DisplayDevice::getHeight() const {
Mathias Agopiana4912602012-07-12 14:25:33 -0700171 return mDisplayHeight;
172}
173
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700174void DisplayDevice::setDisplayName(const String8& displayName) {
175 if (!displayName.isEmpty()) {
176 // never override the name with an empty name
177 mDisplayName = displayName;
178 }
179}
180
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700181uint32_t DisplayDevice::getPageFlipCount() const {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700182 return mPageFlipCount;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800183}
184
Chia-I Wub02087d2017-11-09 10:19:54 -0800185void DisplayDevice::flip() const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800186{
Mathias Agopian875d8e12013-06-07 15:35:48 -0700187 mFlinger->getRenderEngine().checkErrors();
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700188 mPageFlipCount++;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800189}
190
Dan Stoza71433162014-02-04 16:22:36 -0800191status_t DisplayDevice::beginFrame(bool mustRecompose) const {
192 return mDisplaySurface->beginFrame(mustRecompose);
Jesse Hall028dc8f2013-08-20 16:35:32 -0700193}
194
Dan Stoza9e56aa02015-11-02 13:00:03 -0800195status_t DisplayDevice::prepareFrame(HWComposer& hwc) {
196 status_t error = hwc.prepare(*this);
197 if (error != NO_ERROR) {
198 return error;
199 }
200
201 DisplaySurface::CompositionType compositionType;
202 bool hasClient = hwc.hasClientComposition(mHwcDisplayId);
203 bool hasDevice = hwc.hasDeviceComposition(mHwcDisplayId);
204 if (hasClient && hasDevice) {
205 compositionType = DisplaySurface::COMPOSITION_MIXED;
206 } else if (hasClient) {
207 compositionType = DisplaySurface::COMPOSITION_GLES;
208 } else if (hasDevice) {
209 compositionType = DisplaySurface::COMPOSITION_HWC;
210 } else {
211 // Nothing to do -- when turning the screen off we get a frame like
212 // this. Call it a HWC frame since we won't be doing any GLES work but
213 // will do a prepare/set cycle.
214 compositionType = DisplaySurface::COMPOSITION_HWC;
215 }
216 return mDisplaySurface->prepareFrame(compositionType);
217}
Jesse Hall38efe862013-04-06 23:12:29 -0700218
Mathias Agopianda27af92012-09-13 18:17:13 -0700219void DisplayDevice::swapBuffers(HWComposer& hwc) const {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800220 if (hwc.hasClientComposition(mHwcDisplayId)) {
Chia-I Wuf846a352017-11-10 09:22:52 -0800221 mSurface.swapBuffers();
Mathias Agopianda27af92012-09-13 18:17:13 -0700222 }
Mathias Agopian52e21482012-09-24 18:07:21 -0700223
Jesse Hall99c7dbb2013-03-14 14:29:29 -0700224 status_t result = mDisplaySurface->advanceFrame();
225 if (result != NO_ERROR) {
226 ALOGE("[%s] failed pushing new frame to HWC: %d",
227 mDisplayName.string(), result);
Mathias Agopian32341382012-09-25 19:16:28 -0700228 }
Mathias Agopianda27af92012-09-13 18:17:13 -0700229}
230
Dan Stoza9e56aa02015-11-02 13:00:03 -0800231void DisplayDevice::onSwapBuffersCompleted() const {
232 mDisplaySurface->onFrameCommitted();
233}
Mathias Agopianda27af92012-09-13 18:17:13 -0700234
Chia-I Wuf846a352017-11-10 09:22:52 -0800235bool DisplayDevice::makeCurrent() const {
236 bool success = mFlinger->getRenderEngine().setCurrentSurface(mSurface);
Mathias Agopian931bda12013-08-28 18:11:46 -0700237 setViewportAndProjection();
Chia-I Wuf846a352017-11-10 09:22:52 -0800238 return success;
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700239}
240
Mathias Agopian875d8e12013-06-07 15:35:48 -0700241void DisplayDevice::setViewportAndProjection() const {
242 size_t w = mDisplayWidth;
243 size_t h = mDisplayHeight;
Dan Stozac1879002014-05-22 15:59:05 -0700244 Rect sourceCrop(0, 0, w, h);
Riley Andrewsc3ebe662014-09-04 16:20:31 -0700245 mFlinger->getRenderEngine().setViewportAndProjection(w, h, sourceCrop, h,
246 false, Transform::ROT_0);
Mathias Agopianbae92d02012-09-28 01:00:47 -0700247}
248
Dan Stoza9e56aa02015-11-02 13:00:03 -0800249const sp<Fence>& DisplayDevice::getClientTargetAcquireFence() const {
250 return mDisplaySurface->getClientTargetAcquireFence();
251}
Dan Stoza9e56aa02015-11-02 13:00:03 -0800252
Mathias Agopian1b031492012-06-20 17:51:20 -0700253// ----------------------------------------------------------------------------
254
Mathias Agopian13127d82013-03-05 17:47:11 -0800255void DisplayDevice::setVisibleLayersSortedByZ(const Vector< sp<Layer> >& layers) {
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700256 mVisibleLayersSortedByZ = layers;
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700257}
258
Mathias Agopian13127d82013-03-05 17:47:11 -0800259const Vector< sp<Layer> >& DisplayDevice::getVisibleLayersSortedByZ() const {
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700260 return mVisibleLayersSortedByZ;
261}
262
Chia-I Wu83806892017-11-16 10:50:20 -0800263void DisplayDevice::setLayersNeedingFences(const Vector< sp<Layer> >& layers) {
264 mLayersNeedingFences = layers;
265}
266
267const Vector< sp<Layer> >& DisplayDevice::getLayersNeedingFences() const {
268 return mLayersNeedingFences;
269}
270
Mathias Agopiancd60f992012-08-16 16:28:27 -0700271Region DisplayDevice::getDirtyRegion(bool repaintEverything) const {
272 Region dirty;
Mathias Agopiancd60f992012-08-16 16:28:27 -0700273 if (repaintEverything) {
274 dirty.set(getBounds());
275 } else {
Mathias Agopianda8d0a52012-09-04 15:05:38 -0700276 const Transform& planeTransform(mGlobalTransform);
Mathias Agopiancd60f992012-08-16 16:28:27 -0700277 dirty = planeTransform.transform(this->dirtyRegion);
278 dirty.andSelf(getBounds());
279 }
280 return dirty;
281}
282
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700283// ----------------------------------------------------------------------------
Prashant Malani2c9b11f2014-05-25 01:36:31 -0700284void DisplayDevice::setPowerMode(int mode) {
285 mPowerMode = mode;
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700286}
287
Prashant Malani2c9b11f2014-05-25 01:36:31 -0700288int DisplayDevice::getPowerMode() const {
289 return mPowerMode;
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700290}
291
Prashant Malani2c9b11f2014-05-25 01:36:31 -0700292bool DisplayDevice::isDisplayOn() const {
293 return (mPowerMode != HWC_POWER_MODE_OFF);
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700294}
295
296// ----------------------------------------------------------------------------
Michael Lentine6c9e34a2014-07-14 13:48:55 -0700297void DisplayDevice::setActiveConfig(int mode) {
298 mActiveConfig = mode;
299}
300
301int DisplayDevice::getActiveConfig() const {
302 return mActiveConfig;
303}
304
305// ----------------------------------------------------------------------------
Michael Wright28f24d02016-07-12 13:30:53 -0700306void DisplayDevice::setActiveColorMode(android_color_mode_t mode) {
307 mActiveColorMode = mode;
308}
309
310android_color_mode_t DisplayDevice::getActiveColorMode() const {
311 return mActiveColorMode;
312}
Courtney Goeltzenleuchter79d27242017-07-13 17:54:01 -0600313
314void DisplayDevice::setCompositionDataSpace(android_dataspace dataspace) {
315 ANativeWindow* const window = mNativeWindow.get();
316 native_window_set_buffers_data_space(window, dataspace);
317}
Michael Wright28f24d02016-07-12 13:30:53 -0700318
319// ----------------------------------------------------------------------------
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700320
Mathias Agopian28947d72012-08-08 18:51:15 -0700321void DisplayDevice::setLayerStack(uint32_t stack) {
322 mLayerStack = stack;
323 dirtyRegion.set(bounds());
324}
325
326// ----------------------------------------------------------------------------
327
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700328uint32_t DisplayDevice::getOrientationTransform() const {
329 uint32_t transform = 0;
330 switch (mOrientation) {
331 case DisplayState::eOrientationDefault:
332 transform = Transform::ROT_0;
333 break;
334 case DisplayState::eOrientation90:
335 transform = Transform::ROT_90;
336 break;
337 case DisplayState::eOrientation180:
338 transform = Transform::ROT_180;
339 break;
340 case DisplayState::eOrientation270:
341 transform = Transform::ROT_270;
342 break;
343 }
344 return transform;
345}
346
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700347status_t DisplayDevice::orientationToTransfrom(
Mathias Agopian1b031492012-06-20 17:51:20 -0700348 int orientation, int w, int h, Transform* tr)
349{
350 uint32_t flags = 0;
351 switch (orientation) {
Mathias Agopian3165cc22012-08-08 19:42:09 -0700352 case DisplayState::eOrientationDefault:
Mathias Agopian1b031492012-06-20 17:51:20 -0700353 flags = Transform::ROT_0;
354 break;
Mathias Agopian3165cc22012-08-08 19:42:09 -0700355 case DisplayState::eOrientation90:
Mathias Agopian1b031492012-06-20 17:51:20 -0700356 flags = Transform::ROT_90;
357 break;
Mathias Agopian3165cc22012-08-08 19:42:09 -0700358 case DisplayState::eOrientation180:
Mathias Agopian1b031492012-06-20 17:51:20 -0700359 flags = Transform::ROT_180;
360 break;
Mathias Agopian3165cc22012-08-08 19:42:09 -0700361 case DisplayState::eOrientation270:
Mathias Agopian1b031492012-06-20 17:51:20 -0700362 flags = Transform::ROT_270;
363 break;
364 default:
365 return BAD_VALUE;
366 }
367 tr->set(flags, w, h);
368 return NO_ERROR;
369}
370
Michael Lentine47e45402014-07-18 15:34:25 -0700371void DisplayDevice::setDisplaySize(const int newWidth, const int newHeight) {
372 dirtyRegion.set(getBounds());
373
Chia-I Wuf846a352017-11-10 09:22:52 -0800374 mSurface.setNativeWindow(nullptr);
Michael Lentinef2568de2014-08-20 10:51:23 -0700375
Michael Lentine47e45402014-07-18 15:34:25 -0700376 mDisplaySurface->resizeBuffers(newWidth, newHeight);
377
378 ANativeWindow* const window = mNativeWindow.get();
Chia-I Wuf846a352017-11-10 09:22:52 -0800379 mSurface.setNativeWindow(window);
380 mDisplayWidth = mSurface.queryWidth();
381 mDisplayHeight = mSurface.queryHeight();
Michael Lentine47e45402014-07-18 15:34:25 -0700382
383 LOG_FATAL_IF(mDisplayWidth != newWidth,
384 "Unable to set new width to %d", newWidth);
385 LOG_FATAL_IF(mDisplayHeight != newHeight,
386 "Unable to set new height to %d", newHeight);
387}
388
Mathias Agopian00e8c7a2012-09-04 19:30:46 -0700389void DisplayDevice::setProjection(int orientation,
Mathias Agopianf5f714a2013-02-26 16:54:05 -0800390 const Rect& newViewport, const Rect& newFrame) {
391 Rect viewport(newViewport);
392 Rect frame(newFrame);
393
394 const int w = mDisplayWidth;
395 const int h = mDisplayHeight;
396
397 Transform R;
398 DisplayDevice::orientationToTransfrom(orientation, w, h, &R);
399
400 if (!frame.isValid()) {
401 // the destination frame can be invalid if it has never been set,
402 // in that case we assume the whole display frame.
403 frame = Rect(w, h);
404 }
405
406 if (viewport.isEmpty()) {
407 // viewport can be invalid if it has never been set, in that case
408 // we assume the whole display size.
409 // it's also invalid to have an empty viewport, so we handle that
410 // case in the same way.
411 viewport = Rect(w, h);
412 if (R.getOrientation() & Transform::ROT_90) {
413 // viewport is always specified in the logical orientation
414 // of the display (ie: post-rotation).
415 swap(viewport.right, viewport.bottom);
416 }
417 }
418
419 dirtyRegion.set(getBounds());
420
421 Transform TL, TP, S;
422 float src_width = viewport.width();
423 float src_height = viewport.height();
424 float dst_width = frame.width();
425 float dst_height = frame.height();
426 if (src_width != dst_width || src_height != dst_height) {
427 float sx = dst_width / src_width;
428 float sy = dst_height / src_height;
429 S.set(sx, 0, 0, sy);
430 }
431
432 float src_x = viewport.left;
433 float src_y = viewport.top;
434 float dst_x = frame.left;
435 float dst_y = frame.top;
436 TL.set(-src_x, -src_y);
437 TP.set(dst_x, dst_y);
438
439 // The viewport and frame are both in the logical orientation.
440 // Apply the logical translation, scale to physical size, apply the
441 // physical translation and finally rotate to the physical orientation.
442 mGlobalTransform = R * TP * S * TL;
443
444 const uint8_t type = mGlobalTransform.getType();
445 mNeedsFiltering = (!mGlobalTransform.preserveRects() ||
446 (type >= Transform::SCALE));
447
448 mScissor = mGlobalTransform.transform(viewport);
449 if (mScissor.isEmpty()) {
Mathias Agopian6c7f25a2013-05-09 20:37:10 -0700450 mScissor = getBounds();
Mathias Agopianf5f714a2013-02-26 16:54:05 -0800451 }
452
Mathias Agopianda8d0a52012-09-04 15:05:38 -0700453 mOrientation = orientation;
Pablo Ceballos021623b2016-04-15 17:31:51 -0700454 if (mType == DisplayType::DISPLAY_PRIMARY) {
455 uint32_t transform = 0;
456 switch (mOrientation) {
457 case DisplayState::eOrientationDefault:
458 transform = Transform::ROT_0;
459 break;
460 case DisplayState::eOrientation90:
461 transform = Transform::ROT_90;
462 break;
463 case DisplayState::eOrientation180:
464 transform = Transform::ROT_180;
465 break;
466 case DisplayState::eOrientation270:
467 transform = Transform::ROT_270;
468 break;
469 }
470 sPrimaryDisplayOrientation = transform;
471 }
Mathias Agopian00e8c7a2012-09-04 19:30:46 -0700472 mViewport = viewport;
473 mFrame = frame;
Mathias Agopian1b031492012-06-20 17:51:20 -0700474}
Mathias Agopian1d12d8a2012-09-18 01:38:00 -0700475
Pablo Ceballos021623b2016-04-15 17:31:51 -0700476uint32_t DisplayDevice::getPrimaryDisplayOrientationTransform() {
477 return sPrimaryDisplayOrientation;
478}
479
Mathias Agopian74d211a2013-04-22 16:55:35 +0200480void DisplayDevice::dump(String8& result) const {
Mathias Agopian1d12d8a2012-09-18 01:38:00 -0700481 const Transform& tr(mGlobalTransform);
Courtney Goeltzenleuchter152279d2017-08-14 18:18:30 -0600482 ANativeWindow* const window = mNativeWindow.get();
Courtney Goeltzenleuchter0ebaac32017-04-13 12:17:03 -0600483 result.appendFormat("+ DisplayDevice: %s\n", mDisplayName.string());
484 result.appendFormat(" type=%x, hwcId=%d, layerStack=%u, (%4dx%4d), ANativeWindow=%p "
485 "(%d:%d:%d:%d), orient=%2d (type=%08x), "
486 "flips=%u, isSecure=%d, powerMode=%d, activeConfig=%d, numLayers=%zu\n",
Courtney Goeltzenleuchter152279d2017-08-14 18:18:30 -0600487 mType, mHwcDisplayId, mLayerStack, mDisplayWidth, mDisplayHeight, window,
Chia-I Wuf846a352017-11-10 09:22:52 -0800488 mSurface.queryRedSize(), mSurface.queryGreenSize(), mSurface.queryBlueSize(),
489 mSurface.queryAlphaSize(), mOrientation, tr.getType(),
Courtney Goeltzenleuchter152279d2017-08-14 18:18:30 -0600490 getPageFlipCount(), mIsSecure, mPowerMode, mActiveConfig,
Courtney Goeltzenleuchter0ebaac32017-04-13 12:17:03 -0600491 mVisibleLayersSortedByZ.size());
492 result.appendFormat(" v:[%d,%d,%d,%d], f:[%d,%d,%d,%d], s:[%d,%d,%d,%d],"
493 "transform:[[%0.3f,%0.3f,%0.3f][%0.3f,%0.3f,%0.3f][%0.3f,%0.3f,%0.3f]]\n",
494 mViewport.left, mViewport.top, mViewport.right, mViewport.bottom,
495 mFrame.left, mFrame.top, mFrame.right, mFrame.bottom, mScissor.left,
496 mScissor.top, mScissor.right, mScissor.bottom, tr[0][0], tr[1][0], tr[2][0],
497 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 -0600498 auto const surface = static_cast<Surface*>(window);
499 android_dataspace dataspace = surface->getBuffersDataSpace();
500 result.appendFormat(" dataspace: %s (%d)\n", dataspaceDetails(dataspace).c_str(), dataspace);
Mathias Agopian1d12d8a2012-09-18 01:38:00 -0700501
Jesse Hall99c7dbb2013-03-14 14:29:29 -0700502 String8 surfaceDump;
Dan Stozaf10c46e2014-11-11 10:32:31 -0800503 mDisplaySurface->dumpAsString(surfaceDump);
Jesse Hall99c7dbb2013-03-14 14:29:29 -0700504 result.append(surfaceDump);
Mathias Agopian1d12d8a2012-09-18 01:38:00 -0700505}
Irvelffc9efc2016-07-27 15:16:37 -0700506
507std::atomic<int32_t> DisplayDeviceState::nextDisplayId(1);
508
509DisplayDeviceState::DisplayDeviceState(DisplayDevice::DisplayType type, bool isSecure)
510 : type(type),
511 layerStack(DisplayDevice::NO_LAYER_STACK),
512 orientation(0),
513 width(0),
514 height(0),
515 isSecure(isSecure)
516{
517 viewport.makeInvalid();
518 frame.makeInvalid();
519}