blob: 7aa16dede80a91af85aaaa692e83053c0edd5b60 [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),
Mathias Agopian92a979a2012-08-02 18:32:23 -070086 mDisplay(EGL_NO_DISPLAY),
87 mSurface(EGL_NO_SURFACE),
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;
Jesse Hallffe1f192013-03-22 15:13:48 -0700104 /*
105 * Create our display's surface
106 */
107
james.zhang5e8eb5e2015-12-01 17:55:11 +0800108 EGLSurface eglSurface;
Chia-I Wu2b6386e2017-11-09 13:03:17 -0800109 EGLDisplay display = mFlinger->getRenderEngine().getEGLDisplay();
Chia-I Wuc670d502017-11-09 23:17:51 -0800110 EGLConfig config = mFlinger->getRenderEngine().getEGLConfig();
Chia-I Wu2b6386e2017-11-09 13:03:17 -0800111
Jesse Hall19e87292013-12-23 21:02:15 -0800112 if (config == EGL_NO_CONFIG) {
Steven Thomas94e35b92017-07-26 18:48:28 -0700113 config = RenderEngine::chooseEglConfig(display, PIXEL_FORMAT_RGBA_8888,
114 /*logConfig*/ false);
Jesse Hall19e87292013-12-23 21:02:15 -0800115 }
james.zhang5e8eb5e2015-12-01 17:55:11 +0800116 eglSurface = eglCreateWindowSurface(display, config, window, NULL);
117 eglQuerySurface(display, eglSurface, EGL_WIDTH, &mDisplayWidth);
118 eglQuerySurface(display, eglSurface, EGL_HEIGHT, &mDisplayHeight);
Jesse Hallffe1f192013-03-22 15:13:48 -0700119
John Dong4ee56962014-02-21 12:37:59 -0800120 // Make sure that composition can never be stalled by a virtual display
121 // consumer that isn't processing buffers fast enough. We have to do this
122 // in two places:
123 // * Here, in case the display is composed entirely by HWC.
124 // * In makeCurrent(), using eglSwapInterval. Some EGL drivers set the
125 // window's swap interval in eglMakeCurrent, so they'll override the
126 // interval we set here.
127 if (mType >= DisplayDevice::DISPLAY_VIRTUAL)
128 window->setSwapInterval(window, 0);
129
Michael Lentine47e45402014-07-18 15:34:25 -0700130 mConfig = config;
Jesse Hallffe1f192013-03-22 15:13:48 -0700131 mDisplay = display;
james.zhang5e8eb5e2015-12-01 17:55:11 +0800132 mSurface = eglSurface;
Jesse Hallffe1f192013-03-22 15:13:48 -0700133 mPageFlipCount = 0;
134 mViewport.makeInvalid();
135 mFrame.makeInvalid();
136
137 // virtual displays are always considered enabled
Prashant Malani2c9b11f2014-05-25 01:36:31 -0700138 mPowerMode = (mType >= DisplayDevice::DISPLAY_VIRTUAL) ?
139 HWC_POWER_MODE_NORMAL : HWC_POWER_MODE_OFF;
Jesse Hallffe1f192013-03-22 15:13:48 -0700140
141 // Name the display. The name will be replaced shortly if the display
142 // was created with createDisplay().
143 switch (mType) {
144 case DISPLAY_PRIMARY:
145 mDisplayName = "Built-in Screen";
146 break;
147 case DISPLAY_EXTERNAL:
148 mDisplayName = "HDMI Screen";
149 break;
150 default:
151 mDisplayName = "Virtual Screen"; // e.g. Overlay #n
152 break;
153 }
154
155 // initialize the display orientation transform.
156 setProjection(DisplayState::eOrientationDefault, mViewport, mFrame);
james.zhang5e8eb5e2015-12-01 17:55:11 +0800157
Jaesoo Lee720a7242017-01-31 15:26:18 +0900158 if (useTripleFramebuffer) {
159 surface->allocateBuffers();
160 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800161}
162
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700163DisplayDevice::~DisplayDevice() {
Mathias Agopian92a979a2012-08-02 18:32:23 -0700164 if (mSurface != EGL_NO_SURFACE) {
165 eglDestroySurface(mDisplay, mSurface);
166 mSurface = EGL_NO_SURFACE;
167 }
168}
169
Jesse Hall02d86562013-03-25 14:43:23 -0700170void DisplayDevice::disconnect(HWComposer& hwc) {
171 if (mHwcDisplayId >= 0) {
172 hwc.disconnectDisplay(mHwcDisplayId);
Jesse Hall02d86562013-03-25 14:43:23 -0700173 mHwcDisplayId = -1;
174 }
175}
176
Mathias Agopian92a979a2012-08-02 18:32:23 -0700177bool DisplayDevice::isValid() const {
178 return mFlinger != NULL;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800179}
180
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700181int DisplayDevice::getWidth() const {
Mathias Agopiana4912602012-07-12 14:25:33 -0700182 return mDisplayWidth;
183}
184
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700185int DisplayDevice::getHeight() const {
Mathias Agopiana4912602012-07-12 14:25:33 -0700186 return mDisplayHeight;
187}
188
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700189EGLSurface DisplayDevice::getEGLSurface() const {
Mathias Agopiana4912602012-07-12 14:25:33 -0700190 return mSurface;
191}
192
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700193void DisplayDevice::setDisplayName(const String8& displayName) {
194 if (!displayName.isEmpty()) {
195 // never override the name with an empty name
196 mDisplayName = displayName;
197 }
198}
199
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700200uint32_t DisplayDevice::getPageFlipCount() const {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700201 return mPageFlipCount;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800202}
203
Chia-I Wub02087d2017-11-09 10:19:54 -0800204void DisplayDevice::flip() const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800205{
Mathias Agopian875d8e12013-06-07 15:35:48 -0700206 mFlinger->getRenderEngine().checkErrors();
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700207 mPageFlipCount++;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800208}
209
Dan Stoza71433162014-02-04 16:22:36 -0800210status_t DisplayDevice::beginFrame(bool mustRecompose) const {
211 return mDisplaySurface->beginFrame(mustRecompose);
Jesse Hall028dc8f2013-08-20 16:35:32 -0700212}
213
Dan Stoza9e56aa02015-11-02 13:00:03 -0800214status_t DisplayDevice::prepareFrame(HWComposer& hwc) {
215 status_t error = hwc.prepare(*this);
216 if (error != NO_ERROR) {
217 return error;
218 }
219
220 DisplaySurface::CompositionType compositionType;
221 bool hasClient = hwc.hasClientComposition(mHwcDisplayId);
222 bool hasDevice = hwc.hasDeviceComposition(mHwcDisplayId);
223 if (hasClient && hasDevice) {
224 compositionType = DisplaySurface::COMPOSITION_MIXED;
225 } else if (hasClient) {
226 compositionType = DisplaySurface::COMPOSITION_GLES;
227 } else if (hasDevice) {
228 compositionType = DisplaySurface::COMPOSITION_HWC;
229 } else {
230 // Nothing to do -- when turning the screen off we get a frame like
231 // this. Call it a HWC frame since we won't be doing any GLES work but
232 // will do a prepare/set cycle.
233 compositionType = DisplaySurface::COMPOSITION_HWC;
234 }
235 return mDisplaySurface->prepareFrame(compositionType);
236}
Jesse Hall38efe862013-04-06 23:12:29 -0700237
Mathias Agopianda27af92012-09-13 18:17:13 -0700238void DisplayDevice::swapBuffers(HWComposer& hwc) const {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800239 if (hwc.hasClientComposition(mHwcDisplayId)) {
Jesse Hall99c7dbb2013-03-14 14:29:29 -0700240 EGLBoolean success = eglSwapBuffers(mDisplay, mSurface);
241 if (!success) {
242 EGLint error = eglGetError();
243 if (error == EGL_CONTEXT_LOST ||
244 mType == DisplayDevice::DISPLAY_PRIMARY) {
245 LOG_ALWAYS_FATAL("eglSwapBuffers(%p, %p) failed with 0x%08x",
246 mDisplay, mSurface, error);
247 } else {
248 ALOGE("eglSwapBuffers(%p, %p) failed with 0x%08x",
249 mDisplay, mSurface, error);
Mathias Agopianda27af92012-09-13 18:17:13 -0700250 }
251 }
252 }
Mathias Agopian52e21482012-09-24 18:07:21 -0700253
Jesse Hall99c7dbb2013-03-14 14:29:29 -0700254 status_t result = mDisplaySurface->advanceFrame();
255 if (result != NO_ERROR) {
256 ALOGE("[%s] failed pushing new frame to HWC: %d",
257 mDisplayName.string(), result);
Mathias Agopian32341382012-09-25 19:16:28 -0700258 }
Mathias Agopianda27af92012-09-13 18:17:13 -0700259}
260
Dan Stoza9e56aa02015-11-02 13:00:03 -0800261void DisplayDevice::onSwapBuffersCompleted() const {
262 mDisplaySurface->onFrameCommitted();
263}
Mathias Agopianda27af92012-09-13 18:17:13 -0700264
Chia-I Wu7f402902017-11-09 12:51:10 -0800265EGLBoolean DisplayDevice::makeCurrent() const {
Mathias Agopianda8d0a52012-09-04 15:05:38 -0700266 EGLBoolean result = EGL_TRUE;
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700267 EGLSurface sur = eglGetCurrentSurface(EGL_DRAW);
Mathias Agopian875d8e12013-06-07 15:35:48 -0700268 if (sur != mSurface) {
Chia-I Wu7f402902017-11-09 12:51:10 -0800269 result = mFlinger->getRenderEngine().setCurrentSurface(mSurface);
Mathias Agopianda8d0a52012-09-04 15:05:38 -0700270 if (result == EGL_TRUE) {
Jesse Hallf460f552013-08-06 17:08:53 -0700271 if (mType >= DisplayDevice::DISPLAY_VIRTUAL)
Chia-I Wu7f402902017-11-09 12:51:10 -0800272 eglSwapInterval(mDisplay, 0);
Mathias Agopianda8d0a52012-09-04 15:05:38 -0700273 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700274 }
Mathias Agopian931bda12013-08-28 18:11:46 -0700275 setViewportAndProjection();
Mathias Agopianda8d0a52012-09-04 15:05:38 -0700276 return result;
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700277}
278
Mathias Agopian875d8e12013-06-07 15:35:48 -0700279void DisplayDevice::setViewportAndProjection() const {
280 size_t w = mDisplayWidth;
281 size_t h = mDisplayHeight;
Dan Stozac1879002014-05-22 15:59:05 -0700282 Rect sourceCrop(0, 0, w, h);
Riley Andrewsc3ebe662014-09-04 16:20:31 -0700283 mFlinger->getRenderEngine().setViewportAndProjection(w, h, sourceCrop, h,
284 false, Transform::ROT_0);
Mathias Agopianbae92d02012-09-28 01:00:47 -0700285}
286
Dan Stoza9e56aa02015-11-02 13:00:03 -0800287const sp<Fence>& DisplayDevice::getClientTargetAcquireFence() const {
288 return mDisplaySurface->getClientTargetAcquireFence();
289}
Dan Stoza9e56aa02015-11-02 13:00:03 -0800290
Mathias Agopian1b031492012-06-20 17:51:20 -0700291// ----------------------------------------------------------------------------
292
Mathias Agopian13127d82013-03-05 17:47:11 -0800293void DisplayDevice::setVisibleLayersSortedByZ(const Vector< sp<Layer> >& layers) {
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700294 mVisibleLayersSortedByZ = layers;
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700295}
296
Mathias Agopian13127d82013-03-05 17:47:11 -0800297const Vector< sp<Layer> >& DisplayDevice::getVisibleLayersSortedByZ() const {
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700298 return mVisibleLayersSortedByZ;
299}
300
Chia-I Wu83806892017-11-16 10:50:20 -0800301void DisplayDevice::setLayersNeedingFences(const Vector< sp<Layer> >& layers) {
302 mLayersNeedingFences = layers;
303}
304
305const Vector< sp<Layer> >& DisplayDevice::getLayersNeedingFences() const {
306 return mLayersNeedingFences;
307}
308
Mathias Agopiancd60f992012-08-16 16:28:27 -0700309Region DisplayDevice::getDirtyRegion(bool repaintEverything) const {
310 Region dirty;
Mathias Agopiancd60f992012-08-16 16:28:27 -0700311 if (repaintEverything) {
312 dirty.set(getBounds());
313 } else {
Mathias Agopianda8d0a52012-09-04 15:05:38 -0700314 const Transform& planeTransform(mGlobalTransform);
Mathias Agopiancd60f992012-08-16 16:28:27 -0700315 dirty = planeTransform.transform(this->dirtyRegion);
316 dirty.andSelf(getBounds());
317 }
318 return dirty;
319}
320
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700321// ----------------------------------------------------------------------------
Prashant Malani2c9b11f2014-05-25 01:36:31 -0700322void DisplayDevice::setPowerMode(int mode) {
323 mPowerMode = mode;
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700324}
325
Prashant Malani2c9b11f2014-05-25 01:36:31 -0700326int DisplayDevice::getPowerMode() const {
327 return mPowerMode;
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700328}
329
Prashant Malani2c9b11f2014-05-25 01:36:31 -0700330bool DisplayDevice::isDisplayOn() const {
331 return (mPowerMode != HWC_POWER_MODE_OFF);
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700332}
333
334// ----------------------------------------------------------------------------
Michael Lentine6c9e34a2014-07-14 13:48:55 -0700335void DisplayDevice::setActiveConfig(int mode) {
336 mActiveConfig = mode;
337}
338
339int DisplayDevice::getActiveConfig() const {
340 return mActiveConfig;
341}
342
343// ----------------------------------------------------------------------------
Michael Wright28f24d02016-07-12 13:30:53 -0700344void DisplayDevice::setActiveColorMode(android_color_mode_t mode) {
345 mActiveColorMode = mode;
346}
347
348android_color_mode_t DisplayDevice::getActiveColorMode() const {
349 return mActiveColorMode;
350}
Courtney Goeltzenleuchter79d27242017-07-13 17:54:01 -0600351
352void DisplayDevice::setCompositionDataSpace(android_dataspace dataspace) {
353 ANativeWindow* const window = mNativeWindow.get();
354 native_window_set_buffers_data_space(window, dataspace);
355}
Michael Wright28f24d02016-07-12 13:30:53 -0700356
357// ----------------------------------------------------------------------------
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700358
Mathias Agopian28947d72012-08-08 18:51:15 -0700359void DisplayDevice::setLayerStack(uint32_t stack) {
360 mLayerStack = stack;
361 dirtyRegion.set(bounds());
362}
363
364// ----------------------------------------------------------------------------
365
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700366uint32_t DisplayDevice::getOrientationTransform() const {
367 uint32_t transform = 0;
368 switch (mOrientation) {
369 case DisplayState::eOrientationDefault:
370 transform = Transform::ROT_0;
371 break;
372 case DisplayState::eOrientation90:
373 transform = Transform::ROT_90;
374 break;
375 case DisplayState::eOrientation180:
376 transform = Transform::ROT_180;
377 break;
378 case DisplayState::eOrientation270:
379 transform = Transform::ROT_270;
380 break;
381 }
382 return transform;
383}
384
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700385status_t DisplayDevice::orientationToTransfrom(
Mathias Agopian1b031492012-06-20 17:51:20 -0700386 int orientation, int w, int h, Transform* tr)
387{
388 uint32_t flags = 0;
389 switch (orientation) {
Mathias Agopian3165cc22012-08-08 19:42:09 -0700390 case DisplayState::eOrientationDefault:
Mathias Agopian1b031492012-06-20 17:51:20 -0700391 flags = Transform::ROT_0;
392 break;
Mathias Agopian3165cc22012-08-08 19:42:09 -0700393 case DisplayState::eOrientation90:
Mathias Agopian1b031492012-06-20 17:51:20 -0700394 flags = Transform::ROT_90;
395 break;
Mathias Agopian3165cc22012-08-08 19:42:09 -0700396 case DisplayState::eOrientation180:
Mathias Agopian1b031492012-06-20 17:51:20 -0700397 flags = Transform::ROT_180;
398 break;
Mathias Agopian3165cc22012-08-08 19:42:09 -0700399 case DisplayState::eOrientation270:
Mathias Agopian1b031492012-06-20 17:51:20 -0700400 flags = Transform::ROT_270;
401 break;
402 default:
403 return BAD_VALUE;
404 }
405 tr->set(flags, w, h);
406 return NO_ERROR;
407}
408
Michael Lentine47e45402014-07-18 15:34:25 -0700409void DisplayDevice::setDisplaySize(const int newWidth, const int newHeight) {
410 dirtyRegion.set(getBounds());
411
Michael Lentinef2568de2014-08-20 10:51:23 -0700412 if (mSurface != EGL_NO_SURFACE) {
413 eglDestroySurface(mDisplay, mSurface);
414 mSurface = EGL_NO_SURFACE;
415 }
416
Michael Lentine47e45402014-07-18 15:34:25 -0700417 mDisplaySurface->resizeBuffers(newWidth, newHeight);
418
419 ANativeWindow* const window = mNativeWindow.get();
420 mSurface = eglCreateWindowSurface(mDisplay, mConfig, window, NULL);
421 eglQuerySurface(mDisplay, mSurface, EGL_WIDTH, &mDisplayWidth);
422 eglQuerySurface(mDisplay, mSurface, EGL_HEIGHT, &mDisplayHeight);
423
424 LOG_FATAL_IF(mDisplayWidth != newWidth,
425 "Unable to set new width to %d", newWidth);
426 LOG_FATAL_IF(mDisplayHeight != newHeight,
427 "Unable to set new height to %d", newHeight);
428}
429
Mathias Agopian00e8c7a2012-09-04 19:30:46 -0700430void DisplayDevice::setProjection(int orientation,
Mathias Agopianf5f714a2013-02-26 16:54:05 -0800431 const Rect& newViewport, const Rect& newFrame) {
432 Rect viewport(newViewport);
433 Rect frame(newFrame);
434
435 const int w = mDisplayWidth;
436 const int h = mDisplayHeight;
437
438 Transform R;
439 DisplayDevice::orientationToTransfrom(orientation, w, h, &R);
440
441 if (!frame.isValid()) {
442 // the destination frame can be invalid if it has never been set,
443 // in that case we assume the whole display frame.
444 frame = Rect(w, h);
445 }
446
447 if (viewport.isEmpty()) {
448 // viewport can be invalid if it has never been set, in that case
449 // we assume the whole display size.
450 // it's also invalid to have an empty viewport, so we handle that
451 // case in the same way.
452 viewport = Rect(w, h);
453 if (R.getOrientation() & Transform::ROT_90) {
454 // viewport is always specified in the logical orientation
455 // of the display (ie: post-rotation).
456 swap(viewport.right, viewport.bottom);
457 }
458 }
459
460 dirtyRegion.set(getBounds());
461
462 Transform TL, TP, S;
463 float src_width = viewport.width();
464 float src_height = viewport.height();
465 float dst_width = frame.width();
466 float dst_height = frame.height();
467 if (src_width != dst_width || src_height != dst_height) {
468 float sx = dst_width / src_width;
469 float sy = dst_height / src_height;
470 S.set(sx, 0, 0, sy);
471 }
472
473 float src_x = viewport.left;
474 float src_y = viewport.top;
475 float dst_x = frame.left;
476 float dst_y = frame.top;
477 TL.set(-src_x, -src_y);
478 TP.set(dst_x, dst_y);
479
480 // The viewport and frame are both in the logical orientation.
481 // Apply the logical translation, scale to physical size, apply the
482 // physical translation and finally rotate to the physical orientation.
483 mGlobalTransform = R * TP * S * TL;
484
485 const uint8_t type = mGlobalTransform.getType();
486 mNeedsFiltering = (!mGlobalTransform.preserveRects() ||
487 (type >= Transform::SCALE));
488
489 mScissor = mGlobalTransform.transform(viewport);
490 if (mScissor.isEmpty()) {
Mathias Agopian6c7f25a2013-05-09 20:37:10 -0700491 mScissor = getBounds();
Mathias Agopianf5f714a2013-02-26 16:54:05 -0800492 }
493
Mathias Agopianda8d0a52012-09-04 15:05:38 -0700494 mOrientation = orientation;
Pablo Ceballos021623b2016-04-15 17:31:51 -0700495 if (mType == DisplayType::DISPLAY_PRIMARY) {
496 uint32_t transform = 0;
497 switch (mOrientation) {
498 case DisplayState::eOrientationDefault:
499 transform = Transform::ROT_0;
500 break;
501 case DisplayState::eOrientation90:
502 transform = Transform::ROT_90;
503 break;
504 case DisplayState::eOrientation180:
505 transform = Transform::ROT_180;
506 break;
507 case DisplayState::eOrientation270:
508 transform = Transform::ROT_270;
509 break;
510 }
511 sPrimaryDisplayOrientation = transform;
512 }
Mathias Agopian00e8c7a2012-09-04 19:30:46 -0700513 mViewport = viewport;
514 mFrame = frame;
Mathias Agopian1b031492012-06-20 17:51:20 -0700515}
Mathias Agopian1d12d8a2012-09-18 01:38:00 -0700516
Pablo Ceballos021623b2016-04-15 17:31:51 -0700517uint32_t DisplayDevice::getPrimaryDisplayOrientationTransform() {
518 return sPrimaryDisplayOrientation;
519}
520
Mathias Agopian74d211a2013-04-22 16:55:35 +0200521void DisplayDevice::dump(String8& result) const {
Mathias Agopian1d12d8a2012-09-18 01:38:00 -0700522 const Transform& tr(mGlobalTransform);
Courtney Goeltzenleuchter152279d2017-08-14 18:18:30 -0600523 ANativeWindow* const window = mNativeWindow.get();
Courtney Goeltzenleuchter0ebaac32017-04-13 12:17:03 -0600524 EGLint redSize, greenSize, blueSize, alphaSize;
525 eglGetConfigAttrib(mDisplay, mConfig, EGL_RED_SIZE, &redSize);
526 eglGetConfigAttrib(mDisplay, mConfig, EGL_GREEN_SIZE, &greenSize);
527 eglGetConfigAttrib(mDisplay, mConfig, EGL_BLUE_SIZE, &blueSize);
528 eglGetConfigAttrib(mDisplay, mConfig, EGL_ALPHA_SIZE, &alphaSize);
529 result.appendFormat("+ DisplayDevice: %s\n", mDisplayName.string());
530 result.appendFormat(" type=%x, hwcId=%d, layerStack=%u, (%4dx%4d), ANativeWindow=%p "
531 "(%d:%d:%d:%d), orient=%2d (type=%08x), "
532 "flips=%u, isSecure=%d, powerMode=%d, activeConfig=%d, numLayers=%zu\n",
Courtney Goeltzenleuchter152279d2017-08-14 18:18:30 -0600533 mType, mHwcDisplayId, mLayerStack, mDisplayWidth, mDisplayHeight, window,
534 redSize, greenSize, blueSize, alphaSize, mOrientation, tr.getType(),
535 getPageFlipCount(), mIsSecure, mPowerMode, mActiveConfig,
Courtney Goeltzenleuchter0ebaac32017-04-13 12:17:03 -0600536 mVisibleLayersSortedByZ.size());
537 result.appendFormat(" v:[%d,%d,%d,%d], f:[%d,%d,%d,%d], s:[%d,%d,%d,%d],"
538 "transform:[[%0.3f,%0.3f,%0.3f][%0.3f,%0.3f,%0.3f][%0.3f,%0.3f,%0.3f]]\n",
539 mViewport.left, mViewport.top, mViewport.right, mViewport.bottom,
540 mFrame.left, mFrame.top, mFrame.right, mFrame.bottom, mScissor.left,
541 mScissor.top, mScissor.right, mScissor.bottom, tr[0][0], tr[1][0], tr[2][0],
542 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 -0600543 auto const surface = static_cast<Surface*>(window);
544 android_dataspace dataspace = surface->getBuffersDataSpace();
545 result.appendFormat(" dataspace: %s (%d)\n", dataspaceDetails(dataspace).c_str(), dataspace);
Mathias Agopian1d12d8a2012-09-18 01:38:00 -0700546
Jesse Hall99c7dbb2013-03-14 14:29:29 -0700547 String8 surfaceDump;
Dan Stozaf10c46e2014-11-11 10:32:31 -0800548 mDisplaySurface->dumpAsString(surfaceDump);
Jesse Hall99c7dbb2013-03-14 14:29:29 -0700549 result.append(surfaceDump);
Mathias Agopian1d12d8a2012-09-18 01:38:00 -0700550}
Irvelffc9efc2016-07-27 15:16:37 -0700551
552std::atomic<int32_t> DisplayDeviceState::nextDisplayId(1);
553
554DisplayDeviceState::DisplayDeviceState(DisplayDevice::DisplayType type, bool isSecure)
555 : type(type),
556 layerStack(DisplayDevice::NO_LAYER_STACK),
557 orientation(0),
558 width(0),
559 height(0),
560 isSecure(isSecure)
561{
562 viewport.makeInvalid();
563 frame.makeInvalid();
564}