blob: d40666e6f1c01fefed55eeb731d44d282e0eebba [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,
Chia-I Wu5c6e4632018-01-11 08:54:38 -080079 bool supportWideColor,
80 bool supportHdr)
Jesse Hallb7a05492014-08-14 15:45:06 -070081 : lastCompositionHadVisibleLayers(false),
82 mFlinger(flinger),
Dan Stoza9e56aa02015-11-02 13:00:03 -080083 mType(type),
84 mHwcDisplayId(hwcId),
Chih-Wei Huang27e25622013-01-07 17:33:56 +080085 mDisplayToken(displayToken),
Jesse Hall99c7dbb2013-03-14 14:29:29 -070086 mDisplaySurface(displaySurface),
Lloyd Pique144e1162017-12-20 16:44:52 -080087 mSurface{flinger->getRenderEngine().createSurface()},
Dan Stoza9e56aa02015-11-02 13:00:03 -080088 mDisplayWidth(),
89 mDisplayHeight(),
Mathias Agopian92a979a2012-08-02 18:32:23 -070090 mPageFlipCount(),
Jamie Gennisdd3cb842012-10-19 18:19:11 -070091 mIsSecure(isSecure),
Jesse Hall01e29052013-02-19 16:13:35 -080092 mLayerStack(NO_LAYER_STACK),
Prashant Malani2c9b11f2014-05-25 01:36:31 -070093 mOrientation(),
Michael Lentine6c9e34a2014-07-14 13:48:55 -070094 mPowerMode(HWC_POWER_MODE_OFF),
Courtney Goeltzenleuchter4f20f9c2017-04-06 08:18:34 -060095 mActiveConfig(0)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080096{
Courtney Goeltzenleuchter5d943892017-03-22 13:46:46 -060097 // clang-format on
james.zhang5e8eb5e2015-12-01 17:55:11 +080098 Surface* surface;
99 mNativeWindow = surface = new Surface(producer, false);
Jesse Hallffe1f192013-03-22 15:13:48 -0700100 ANativeWindow* const window = mNativeWindow.get();
101
Naseer Ahmed901a86e2017-05-11 11:15:21 -0400102 mActiveColorMode = HAL_COLOR_MODE_NATIVE;
Courtney Goeltzenleuchter4f20f9c2017-04-06 08:18:34 -0600103 mDisplayHasWideColor = supportWideColor;
Chia-I Wu5c6e4632018-01-11 08:54:38 -0800104 mDisplayHasHdr = supportHdr;
105
Jesse Hallffe1f192013-03-22 15:13:48 -0700106 /*
107 * Create our display's surface
108 */
Lloyd Pique144e1162017-12-20 16:44:52 -0800109 mSurface->setCritical(mType == DisplayDevice::DISPLAY_PRIMARY);
110 mSurface->setAsync(mType >= DisplayDevice::DISPLAY_VIRTUAL);
111 mSurface->setNativeWindow(window);
112 mDisplayWidth = mSurface->queryWidth();
113 mDisplayHeight = mSurface->queryHeight();
Jesse Hallffe1f192013-03-22 15:13:48 -0700114
John Dong4ee56962014-02-21 12:37:59 -0800115 // Make sure that composition can never be stalled by a virtual display
116 // consumer that isn't processing buffers fast enough. We have to do this
117 // in two places:
118 // * Here, in case the display is composed entirely by HWC.
119 // * In makeCurrent(), using eglSwapInterval. Some EGL drivers set the
120 // window's swap interval in eglMakeCurrent, so they'll override the
121 // interval we set here.
122 if (mType >= DisplayDevice::DISPLAY_VIRTUAL)
123 window->setSwapInterval(window, 0);
124
Jesse Hallffe1f192013-03-22 15:13:48 -0700125 mPageFlipCount = 0;
126 mViewport.makeInvalid();
127 mFrame.makeInvalid();
128
129 // virtual displays are always considered enabled
Prashant Malani2c9b11f2014-05-25 01:36:31 -0700130 mPowerMode = (mType >= DisplayDevice::DISPLAY_VIRTUAL) ?
131 HWC_POWER_MODE_NORMAL : HWC_POWER_MODE_OFF;
Jesse Hallffe1f192013-03-22 15:13:48 -0700132
Jesse Hallffe1f192013-03-22 15:13:48 -0700133 // initialize the display orientation transform.
134 setProjection(DisplayState::eOrientationDefault, mViewport, mFrame);
james.zhang5e8eb5e2015-12-01 17:55:11 +0800135
Jaesoo Lee720a7242017-01-31 15:26:18 +0900136 if (useTripleFramebuffer) {
137 surface->allocateBuffers();
138 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800139}
140
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700141DisplayDevice::~DisplayDevice() {
Mathias Agopian92a979a2012-08-02 18:32:23 -0700142}
143
Jesse Hall02d86562013-03-25 14:43:23 -0700144void DisplayDevice::disconnect(HWComposer& hwc) {
145 if (mHwcDisplayId >= 0) {
146 hwc.disconnectDisplay(mHwcDisplayId);
Jesse Hall02d86562013-03-25 14:43:23 -0700147 mHwcDisplayId = -1;
148 }
149}
150
Mathias Agopian92a979a2012-08-02 18:32:23 -0700151bool DisplayDevice::isValid() const {
Peiyong Lin566a3b42018-01-09 18:22:43 -0800152 return mFlinger != nullptr;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800153}
154
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700155int DisplayDevice::getWidth() const {
Mathias Agopiana4912602012-07-12 14:25:33 -0700156 return mDisplayWidth;
157}
158
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700159int DisplayDevice::getHeight() const {
Mathias Agopiana4912602012-07-12 14:25:33 -0700160 return mDisplayHeight;
161}
162
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700163void DisplayDevice::setDisplayName(const String8& displayName) {
164 if (!displayName.isEmpty()) {
165 // never override the name with an empty name
166 mDisplayName = displayName;
167 }
168}
169
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700170uint32_t DisplayDevice::getPageFlipCount() const {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700171 return mPageFlipCount;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800172}
173
Chia-I Wub02087d2017-11-09 10:19:54 -0800174void DisplayDevice::flip() const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800175{
Mathias Agopian875d8e12013-06-07 15:35:48 -0700176 mFlinger->getRenderEngine().checkErrors();
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700177 mPageFlipCount++;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800178}
179
Dan Stoza71433162014-02-04 16:22:36 -0800180status_t DisplayDevice::beginFrame(bool mustRecompose) const {
181 return mDisplaySurface->beginFrame(mustRecompose);
Jesse Hall028dc8f2013-08-20 16:35:32 -0700182}
183
Dan Stoza9e56aa02015-11-02 13:00:03 -0800184status_t DisplayDevice::prepareFrame(HWComposer& hwc) {
185 status_t error = hwc.prepare(*this);
186 if (error != NO_ERROR) {
187 return error;
188 }
189
190 DisplaySurface::CompositionType compositionType;
191 bool hasClient = hwc.hasClientComposition(mHwcDisplayId);
192 bool hasDevice = hwc.hasDeviceComposition(mHwcDisplayId);
193 if (hasClient && hasDevice) {
194 compositionType = DisplaySurface::COMPOSITION_MIXED;
195 } else if (hasClient) {
196 compositionType = DisplaySurface::COMPOSITION_GLES;
197 } else if (hasDevice) {
198 compositionType = DisplaySurface::COMPOSITION_HWC;
199 } else {
200 // Nothing to do -- when turning the screen off we get a frame like
201 // this. Call it a HWC frame since we won't be doing any GLES work but
202 // will do a prepare/set cycle.
203 compositionType = DisplaySurface::COMPOSITION_HWC;
204 }
205 return mDisplaySurface->prepareFrame(compositionType);
206}
Jesse Hall38efe862013-04-06 23:12:29 -0700207
Mathias Agopianda27af92012-09-13 18:17:13 -0700208void DisplayDevice::swapBuffers(HWComposer& hwc) const {
Madhuri Athota88a905b2017-05-04 16:58:15 +0530209 if (hwc.hasClientComposition(mHwcDisplayId) || hwc.hasFlipClientTargetRequest(mHwcDisplayId)) {
Lloyd Pique144e1162017-12-20 16:44:52 -0800210 mSurface->swapBuffers();
Mathias Agopianda27af92012-09-13 18:17:13 -0700211 }
Mathias Agopian52e21482012-09-24 18:07:21 -0700212
Jesse Hall99c7dbb2013-03-14 14:29:29 -0700213 status_t result = mDisplaySurface->advanceFrame();
214 if (result != NO_ERROR) {
215 ALOGE("[%s] failed pushing new frame to HWC: %d",
216 mDisplayName.string(), result);
Mathias Agopian32341382012-09-25 19:16:28 -0700217 }
Mathias Agopianda27af92012-09-13 18:17:13 -0700218}
219
Dan Stoza9e56aa02015-11-02 13:00:03 -0800220void DisplayDevice::onSwapBuffersCompleted() const {
221 mDisplaySurface->onFrameCommitted();
222}
Mathias Agopianda27af92012-09-13 18:17:13 -0700223
Chia-I Wuf846a352017-11-10 09:22:52 -0800224bool DisplayDevice::makeCurrent() const {
Lloyd Pique144e1162017-12-20 16:44:52 -0800225 bool success = mFlinger->getRenderEngine().setCurrentSurface(*mSurface);
Mathias Agopian931bda12013-08-28 18:11:46 -0700226 setViewportAndProjection();
Chia-I Wuf846a352017-11-10 09:22:52 -0800227 return success;
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700228}
229
Mathias Agopian875d8e12013-06-07 15:35:48 -0700230void DisplayDevice::setViewportAndProjection() const {
231 size_t w = mDisplayWidth;
232 size_t h = mDisplayHeight;
Dan Stozac1879002014-05-22 15:59:05 -0700233 Rect sourceCrop(0, 0, w, h);
Riley Andrewsc3ebe662014-09-04 16:20:31 -0700234 mFlinger->getRenderEngine().setViewportAndProjection(w, h, sourceCrop, h,
235 false, Transform::ROT_0);
Mathias Agopianbae92d02012-09-28 01:00:47 -0700236}
237
Dan Stoza9e56aa02015-11-02 13:00:03 -0800238const sp<Fence>& DisplayDevice::getClientTargetAcquireFence() const {
239 return mDisplaySurface->getClientTargetAcquireFence();
240}
Dan Stoza9e56aa02015-11-02 13:00:03 -0800241
Mathias Agopian1b031492012-06-20 17:51:20 -0700242// ----------------------------------------------------------------------------
243
Mathias Agopian13127d82013-03-05 17:47:11 -0800244void DisplayDevice::setVisibleLayersSortedByZ(const Vector< sp<Layer> >& layers) {
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700245 mVisibleLayersSortedByZ = layers;
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700246}
247
Mathias Agopian13127d82013-03-05 17:47:11 -0800248const Vector< sp<Layer> >& DisplayDevice::getVisibleLayersSortedByZ() const {
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700249 return mVisibleLayersSortedByZ;
250}
251
Chia-I Wu83806892017-11-16 10:50:20 -0800252void DisplayDevice::setLayersNeedingFences(const Vector< sp<Layer> >& layers) {
253 mLayersNeedingFences = layers;
254}
255
256const Vector< sp<Layer> >& DisplayDevice::getLayersNeedingFences() const {
257 return mLayersNeedingFences;
258}
259
Mathias Agopiancd60f992012-08-16 16:28:27 -0700260Region DisplayDevice::getDirtyRegion(bool repaintEverything) const {
261 Region dirty;
Mathias Agopiancd60f992012-08-16 16:28:27 -0700262 if (repaintEverything) {
263 dirty.set(getBounds());
264 } else {
Mathias Agopianda8d0a52012-09-04 15:05:38 -0700265 const Transform& planeTransform(mGlobalTransform);
Mathias Agopiancd60f992012-08-16 16:28:27 -0700266 dirty = planeTransform.transform(this->dirtyRegion);
267 dirty.andSelf(getBounds());
268 }
269 return dirty;
270}
271
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700272// ----------------------------------------------------------------------------
Prashant Malani2c9b11f2014-05-25 01:36:31 -0700273void DisplayDevice::setPowerMode(int mode) {
274 mPowerMode = mode;
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700275}
276
Prashant Malani2c9b11f2014-05-25 01:36:31 -0700277int DisplayDevice::getPowerMode() const {
278 return mPowerMode;
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700279}
280
Prashant Malani2c9b11f2014-05-25 01:36:31 -0700281bool DisplayDevice::isDisplayOn() const {
282 return (mPowerMode != HWC_POWER_MODE_OFF);
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700283}
284
285// ----------------------------------------------------------------------------
Michael Lentine6c9e34a2014-07-14 13:48:55 -0700286void DisplayDevice::setActiveConfig(int mode) {
287 mActiveConfig = mode;
288}
289
290int DisplayDevice::getActiveConfig() const {
291 return mActiveConfig;
292}
293
294// ----------------------------------------------------------------------------
Michael Wright28f24d02016-07-12 13:30:53 -0700295void DisplayDevice::setActiveColorMode(android_color_mode_t mode) {
296 mActiveColorMode = mode;
297}
298
299android_color_mode_t DisplayDevice::getActiveColorMode() const {
300 return mActiveColorMode;
301}
Courtney Goeltzenleuchter79d27242017-07-13 17:54:01 -0600302
303void DisplayDevice::setCompositionDataSpace(android_dataspace dataspace) {
304 ANativeWindow* const window = mNativeWindow.get();
305 native_window_set_buffers_data_space(window, dataspace);
306}
Michael Wright28f24d02016-07-12 13:30:53 -0700307
308// ----------------------------------------------------------------------------
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700309
Mathias Agopian28947d72012-08-08 18:51:15 -0700310void DisplayDevice::setLayerStack(uint32_t stack) {
311 mLayerStack = stack;
312 dirtyRegion.set(bounds());
313}
314
315// ----------------------------------------------------------------------------
316
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700317uint32_t DisplayDevice::getOrientationTransform() const {
318 uint32_t transform = 0;
319 switch (mOrientation) {
320 case DisplayState::eOrientationDefault:
321 transform = Transform::ROT_0;
322 break;
323 case DisplayState::eOrientation90:
324 transform = Transform::ROT_90;
325 break;
326 case DisplayState::eOrientation180:
327 transform = Transform::ROT_180;
328 break;
329 case DisplayState::eOrientation270:
330 transform = Transform::ROT_270;
331 break;
332 }
333 return transform;
334}
335
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700336status_t DisplayDevice::orientationToTransfrom(
Mathias Agopian1b031492012-06-20 17:51:20 -0700337 int orientation, int w, int h, Transform* tr)
338{
339 uint32_t flags = 0;
340 switch (orientation) {
Mathias Agopian3165cc22012-08-08 19:42:09 -0700341 case DisplayState::eOrientationDefault:
Mathias Agopian1b031492012-06-20 17:51:20 -0700342 flags = Transform::ROT_0;
343 break;
Mathias Agopian3165cc22012-08-08 19:42:09 -0700344 case DisplayState::eOrientation90:
Mathias Agopian1b031492012-06-20 17:51:20 -0700345 flags = Transform::ROT_90;
346 break;
Mathias Agopian3165cc22012-08-08 19:42:09 -0700347 case DisplayState::eOrientation180:
Mathias Agopian1b031492012-06-20 17:51:20 -0700348 flags = Transform::ROT_180;
349 break;
Mathias Agopian3165cc22012-08-08 19:42:09 -0700350 case DisplayState::eOrientation270:
Mathias Agopian1b031492012-06-20 17:51:20 -0700351 flags = Transform::ROT_270;
352 break;
353 default:
354 return BAD_VALUE;
355 }
356 tr->set(flags, w, h);
357 return NO_ERROR;
358}
359
Michael Lentine47e45402014-07-18 15:34:25 -0700360void DisplayDevice::setDisplaySize(const int newWidth, const int newHeight) {
361 dirtyRegion.set(getBounds());
362
Lloyd Pique144e1162017-12-20 16:44:52 -0800363 mSurface->setNativeWindow(nullptr);
Michael Lentinef2568de2014-08-20 10:51:23 -0700364
Michael Lentine47e45402014-07-18 15:34:25 -0700365 mDisplaySurface->resizeBuffers(newWidth, newHeight);
366
367 ANativeWindow* const window = mNativeWindow.get();
Lloyd Pique144e1162017-12-20 16:44:52 -0800368 mSurface->setNativeWindow(window);
369 mDisplayWidth = mSurface->queryWidth();
370 mDisplayHeight = mSurface->queryHeight();
Michael Lentine47e45402014-07-18 15:34:25 -0700371
372 LOG_FATAL_IF(mDisplayWidth != newWidth,
373 "Unable to set new width to %d", newWidth);
374 LOG_FATAL_IF(mDisplayHeight != newHeight,
375 "Unable to set new height to %d", newHeight);
376}
377
Mathias Agopian00e8c7a2012-09-04 19:30:46 -0700378void DisplayDevice::setProjection(int orientation,
Mathias Agopianf5f714a2013-02-26 16:54:05 -0800379 const Rect& newViewport, const Rect& newFrame) {
380 Rect viewport(newViewport);
381 Rect frame(newFrame);
382
383 const int w = mDisplayWidth;
384 const int h = mDisplayHeight;
385
386 Transform R;
387 DisplayDevice::orientationToTransfrom(orientation, w, h, &R);
388
389 if (!frame.isValid()) {
390 // the destination frame can be invalid if it has never been set,
391 // in that case we assume the whole display frame.
392 frame = Rect(w, h);
393 }
394
395 if (viewport.isEmpty()) {
396 // viewport can be invalid if it has never been set, in that case
397 // we assume the whole display size.
398 // it's also invalid to have an empty viewport, so we handle that
399 // case in the same way.
400 viewport = Rect(w, h);
401 if (R.getOrientation() & Transform::ROT_90) {
402 // viewport is always specified in the logical orientation
403 // of the display (ie: post-rotation).
404 swap(viewport.right, viewport.bottom);
405 }
406 }
407
408 dirtyRegion.set(getBounds());
409
410 Transform TL, TP, S;
411 float src_width = viewport.width();
412 float src_height = viewport.height();
413 float dst_width = frame.width();
414 float dst_height = frame.height();
415 if (src_width != dst_width || src_height != dst_height) {
416 float sx = dst_width / src_width;
417 float sy = dst_height / src_height;
418 S.set(sx, 0, 0, sy);
419 }
420
421 float src_x = viewport.left;
422 float src_y = viewport.top;
423 float dst_x = frame.left;
424 float dst_y = frame.top;
425 TL.set(-src_x, -src_y);
426 TP.set(dst_x, dst_y);
427
428 // The viewport and frame are both in the logical orientation.
429 // Apply the logical translation, scale to physical size, apply the
430 // physical translation and finally rotate to the physical orientation.
431 mGlobalTransform = R * TP * S * TL;
432
433 const uint8_t type = mGlobalTransform.getType();
434 mNeedsFiltering = (!mGlobalTransform.preserveRects() ||
435 (type >= Transform::SCALE));
436
437 mScissor = mGlobalTransform.transform(viewport);
438 if (mScissor.isEmpty()) {
Mathias Agopian6c7f25a2013-05-09 20:37:10 -0700439 mScissor = getBounds();
Mathias Agopianf5f714a2013-02-26 16:54:05 -0800440 }
441
Mathias Agopianda8d0a52012-09-04 15:05:38 -0700442 mOrientation = orientation;
Pablo Ceballos021623b2016-04-15 17:31:51 -0700443 if (mType == DisplayType::DISPLAY_PRIMARY) {
444 uint32_t transform = 0;
445 switch (mOrientation) {
446 case DisplayState::eOrientationDefault:
447 transform = Transform::ROT_0;
448 break;
449 case DisplayState::eOrientation90:
450 transform = Transform::ROT_90;
451 break;
452 case DisplayState::eOrientation180:
453 transform = Transform::ROT_180;
454 break;
455 case DisplayState::eOrientation270:
456 transform = Transform::ROT_270;
457 break;
458 }
459 sPrimaryDisplayOrientation = transform;
460 }
Mathias Agopian00e8c7a2012-09-04 19:30:46 -0700461 mViewport = viewport;
462 mFrame = frame;
Mathias Agopian1b031492012-06-20 17:51:20 -0700463}
Mathias Agopian1d12d8a2012-09-18 01:38:00 -0700464
Pablo Ceballos021623b2016-04-15 17:31:51 -0700465uint32_t DisplayDevice::getPrimaryDisplayOrientationTransform() {
466 return sPrimaryDisplayOrientation;
467}
468
Mathias Agopian74d211a2013-04-22 16:55:35 +0200469void DisplayDevice::dump(String8& result) const {
Mathias Agopian1d12d8a2012-09-18 01:38:00 -0700470 const Transform& tr(mGlobalTransform);
Courtney Goeltzenleuchter152279d2017-08-14 18:18:30 -0600471 ANativeWindow* const window = mNativeWindow.get();
Courtney Goeltzenleuchter0ebaac32017-04-13 12:17:03 -0600472 result.appendFormat("+ DisplayDevice: %s\n", mDisplayName.string());
473 result.appendFormat(" type=%x, hwcId=%d, layerStack=%u, (%4dx%4d), ANativeWindow=%p "
474 "(%d:%d:%d:%d), orient=%2d (type=%08x), "
475 "flips=%u, isSecure=%d, powerMode=%d, activeConfig=%d, numLayers=%zu\n",
Courtney Goeltzenleuchter152279d2017-08-14 18:18:30 -0600476 mType, mHwcDisplayId, mLayerStack, mDisplayWidth, mDisplayHeight, window,
Lloyd Pique144e1162017-12-20 16:44:52 -0800477 mSurface->queryRedSize(), mSurface->queryGreenSize(),
478 mSurface->queryBlueSize(), mSurface->queryAlphaSize(), mOrientation,
479 tr.getType(), getPageFlipCount(), mIsSecure, mPowerMode, mActiveConfig,
Courtney Goeltzenleuchter0ebaac32017-04-13 12:17:03 -0600480 mVisibleLayersSortedByZ.size());
481 result.appendFormat(" v:[%d,%d,%d,%d], f:[%d,%d,%d,%d], s:[%d,%d,%d,%d],"
482 "transform:[[%0.3f,%0.3f,%0.3f][%0.3f,%0.3f,%0.3f][%0.3f,%0.3f,%0.3f]]\n",
483 mViewport.left, mViewport.top, mViewport.right, mViewport.bottom,
484 mFrame.left, mFrame.top, mFrame.right, mFrame.bottom, mScissor.left,
485 mScissor.top, mScissor.right, mScissor.bottom, tr[0][0], tr[1][0], tr[2][0],
486 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 -0600487 auto const surface = static_cast<Surface*>(window);
488 android_dataspace dataspace = surface->getBuffersDataSpace();
489 result.appendFormat(" dataspace: %s (%d)\n", dataspaceDetails(dataspace).c_str(), dataspace);
Mathias Agopian1d12d8a2012-09-18 01:38:00 -0700490
Jesse Hall99c7dbb2013-03-14 14:29:29 -0700491 String8 surfaceDump;
Dan Stozaf10c46e2014-11-11 10:32:31 -0800492 mDisplaySurface->dumpAsString(surfaceDump);
Jesse Hall99c7dbb2013-03-14 14:29:29 -0700493 result.append(surfaceDump);
Mathias Agopian1d12d8a2012-09-18 01:38:00 -0700494}
Irvelffc9efc2016-07-27 15:16:37 -0700495
496std::atomic<int32_t> DisplayDeviceState::nextDisplayId(1);
497
498DisplayDeviceState::DisplayDeviceState(DisplayDevice::DisplayType type, bool isSecure)
499 : type(type),
500 layerStack(DisplayDevice::NO_LAYER_STACK),
501 orientation(0),
502 width(0),
503 height(0),
504 isSecure(isSecure)
505{
506 viewport.makeInvalid();
507 frame.makeInvalid();
508}