blob: 6d6781e4d479ab2429915a7366803b100a0da13f [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
Andreas Gampe89fd4f72014-11-13 14:18:56 -080056#ifdef EGL_ANDROID_swap_rectangle
57static constexpr bool kEGLAndroidSwapRectangle = true;
58#else
59static constexpr bool kEGLAndroidSwapRectangle = false;
60#endif
61
Jaesoo Lee720a7242017-01-31 15:26:18 +090062// retrieve triple buffer setting from configstore
63using namespace android::hardware::configstore;
64using namespace android::hardware::configstore::V1_0;
65
Fabien Sanglard1971b632017-03-10 14:50:03 -080066static bool useTripleFramebuffer = getInt64< ISurfaceFlingerConfigs,
Fabien Sanglard48eb5652017-07-26 14:55:11 -070067 &ISurfaceFlingerConfigs::maxFrameBufferAcquiredBuffers>(2) >= 3;
Jaesoo Lee720a7242017-01-31 15:26:18 +090068
Andreas Gampe89fd4f72014-11-13 14:18:56 -080069#if !defined(EGL_EGLEXT_PROTOTYPES) || !defined(EGL_ANDROID_swap_rectangle)
70// Dummy implementation in case it is missing.
71inline void eglSetSwapRectangleANDROID (EGLDisplay, EGLSurface, EGLint, EGLint, EGLint, EGLint) {
72}
73#endif
74
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080075/*
76 * Initialize the display to the specified values.
77 *
78 */
79
Pablo Ceballos021623b2016-04-15 17:31:51 -070080uint32_t DisplayDevice::sPrimaryDisplayOrientation = 0;
81
Courtney Goeltzenleuchter5d943892017-03-22 13:46:46 -060082// clang-format off
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -070083DisplayDevice::DisplayDevice(
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080084 const sp<SurfaceFlinger>& flinger,
Jamie Gennisdd3cb842012-10-19 18:19:11 -070085 DisplayType type,
Jesse Hallffe1f192013-03-22 15:13:48 -070086 int32_t hwcId,
Jamie Gennisdd3cb842012-10-19 18:19:11 -070087 bool isSecure,
88 const wp<IBinder>& displayToken,
Jesse Hall99c7dbb2013-03-14 14:29:29 -070089 const sp<DisplaySurface>& displaySurface,
Mathias Agopiandb89edc2013-08-02 01:40:18 -070090 const sp<IGraphicBufferProducer>& producer,
Courtney Goeltzenleuchter5d943892017-03-22 13:46:46 -060091 EGLConfig config,
92 bool supportWideColor)
Jesse Hallb7a05492014-08-14 15:45:06 -070093 : lastCompositionHadVisibleLayers(false),
94 mFlinger(flinger),
Dan Stoza9e56aa02015-11-02 13:00:03 -080095 mType(type),
96 mHwcDisplayId(hwcId),
Chih-Wei Huang27e25622013-01-07 17:33:56 +080097 mDisplayToken(displayToken),
Jesse Hall99c7dbb2013-03-14 14:29:29 -070098 mDisplaySurface(displaySurface),
Mathias Agopian92a979a2012-08-02 18:32:23 -070099 mDisplay(EGL_NO_DISPLAY),
100 mSurface(EGL_NO_SURFACE),
Dan Stoza9e56aa02015-11-02 13:00:03 -0800101 mDisplayWidth(),
102 mDisplayHeight(),
Mathias Agopian92a979a2012-08-02 18:32:23 -0700103 mFlags(),
104 mPageFlipCount(),
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700105 mIsSecure(isSecure),
Jesse Hall01e29052013-02-19 16:13:35 -0800106 mLayerStack(NO_LAYER_STACK),
Prashant Malani2c9b11f2014-05-25 01:36:31 -0700107 mOrientation(),
Michael Lentine6c9e34a2014-07-14 13:48:55 -0700108 mPowerMode(HWC_POWER_MODE_OFF),
Courtney Goeltzenleuchter4f20f9c2017-04-06 08:18:34 -0600109 mActiveConfig(0)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800110{
Courtney Goeltzenleuchter5d943892017-03-22 13:46:46 -0600111 // clang-format on
james.zhang5e8eb5e2015-12-01 17:55:11 +0800112 Surface* surface;
113 mNativeWindow = surface = new Surface(producer, false);
Jesse Hallffe1f192013-03-22 15:13:48 -0700114 ANativeWindow* const window = mNativeWindow.get();
115
Naseer Ahmed901a86e2017-05-11 11:15:21 -0400116 mActiveColorMode = HAL_COLOR_MODE_NATIVE;
Courtney Goeltzenleuchter4f20f9c2017-04-06 08:18:34 -0600117 mDisplayHasWideColor = supportWideColor;
Jesse Hallffe1f192013-03-22 15:13:48 -0700118 /*
119 * Create our display's surface
120 */
121
james.zhang5e8eb5e2015-12-01 17:55:11 +0800122 EGLSurface eglSurface;
Jesse Hallffe1f192013-03-22 15:13:48 -0700123 EGLDisplay display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
Jesse Hall19e87292013-12-23 21:02:15 -0800124 if (config == EGL_NO_CONFIG) {
Steven Thomas94e35b92017-07-26 18:48:28 -0700125 config = RenderEngine::chooseEglConfig(display, PIXEL_FORMAT_RGBA_8888,
126 /*logConfig*/ false);
Jesse Hall19e87292013-12-23 21:02:15 -0800127 }
james.zhang5e8eb5e2015-12-01 17:55:11 +0800128 eglSurface = eglCreateWindowSurface(display, config, window, NULL);
129 eglQuerySurface(display, eglSurface, EGL_WIDTH, &mDisplayWidth);
130 eglQuerySurface(display, eglSurface, EGL_HEIGHT, &mDisplayHeight);
Jesse Hallffe1f192013-03-22 15:13:48 -0700131
John Dong4ee56962014-02-21 12:37:59 -0800132 // Make sure that composition can never be stalled by a virtual display
133 // consumer that isn't processing buffers fast enough. We have to do this
134 // in two places:
135 // * Here, in case the display is composed entirely by HWC.
136 // * In makeCurrent(), using eglSwapInterval. Some EGL drivers set the
137 // window's swap interval in eglMakeCurrent, so they'll override the
138 // interval we set here.
139 if (mType >= DisplayDevice::DISPLAY_VIRTUAL)
140 window->setSwapInterval(window, 0);
141
Michael Lentine47e45402014-07-18 15:34:25 -0700142 mConfig = config;
Jesse Hallffe1f192013-03-22 15:13:48 -0700143 mDisplay = display;
james.zhang5e8eb5e2015-12-01 17:55:11 +0800144 mSurface = eglSurface;
Jesse Hallffe1f192013-03-22 15:13:48 -0700145 mPageFlipCount = 0;
146 mViewport.makeInvalid();
147 mFrame.makeInvalid();
148
149 // virtual displays are always considered enabled
Prashant Malani2c9b11f2014-05-25 01:36:31 -0700150 mPowerMode = (mType >= DisplayDevice::DISPLAY_VIRTUAL) ?
151 HWC_POWER_MODE_NORMAL : HWC_POWER_MODE_OFF;
Jesse Hallffe1f192013-03-22 15:13:48 -0700152
153 // Name the display. The name will be replaced shortly if the display
154 // was created with createDisplay().
155 switch (mType) {
156 case DISPLAY_PRIMARY:
157 mDisplayName = "Built-in Screen";
158 break;
159 case DISPLAY_EXTERNAL:
160 mDisplayName = "HDMI Screen";
161 break;
162 default:
163 mDisplayName = "Virtual Screen"; // e.g. Overlay #n
164 break;
165 }
166
167 // initialize the display orientation transform.
168 setProjection(DisplayState::eOrientationDefault, mViewport, mFrame);
james.zhang5e8eb5e2015-12-01 17:55:11 +0800169
Jaesoo Lee720a7242017-01-31 15:26:18 +0900170 if (useTripleFramebuffer) {
171 surface->allocateBuffers();
172 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800173}
174
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700175DisplayDevice::~DisplayDevice() {
Mathias Agopian92a979a2012-08-02 18:32:23 -0700176 if (mSurface != EGL_NO_SURFACE) {
177 eglDestroySurface(mDisplay, mSurface);
178 mSurface = EGL_NO_SURFACE;
179 }
180}
181
Jesse Hall02d86562013-03-25 14:43:23 -0700182void DisplayDevice::disconnect(HWComposer& hwc) {
183 if (mHwcDisplayId >= 0) {
184 hwc.disconnectDisplay(mHwcDisplayId);
Jesse Hall02d86562013-03-25 14:43:23 -0700185 mHwcDisplayId = -1;
186 }
187}
188
Mathias Agopian92a979a2012-08-02 18:32:23 -0700189bool DisplayDevice::isValid() const {
190 return mFlinger != NULL;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800191}
192
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700193int DisplayDevice::getWidth() const {
Mathias Agopiana4912602012-07-12 14:25:33 -0700194 return mDisplayWidth;
195}
196
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700197int DisplayDevice::getHeight() const {
Mathias Agopiana4912602012-07-12 14:25:33 -0700198 return mDisplayHeight;
199}
200
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700201EGLSurface DisplayDevice::getEGLSurface() const {
Mathias Agopiana4912602012-07-12 14:25:33 -0700202 return mSurface;
203}
204
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700205void DisplayDevice::setDisplayName(const String8& displayName) {
206 if (!displayName.isEmpty()) {
207 // never override the name with an empty name
208 mDisplayName = displayName;
209 }
210}
211
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700212uint32_t DisplayDevice::getPageFlipCount() const {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700213 return mPageFlipCount;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800214}
215
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700216void DisplayDevice::flip(const Region& dirty) const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800217{
Mathias Agopian875d8e12013-06-07 15:35:48 -0700218 mFlinger->getRenderEngine().checkErrors();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800219
Andreas Gampe89fd4f72014-11-13 14:18:56 -0800220 if (kEGLAndroidSwapRectangle) {
221 if (mFlags & SWAP_RECTANGLE) {
222 const Region newDirty(dirty.intersect(bounds()));
223 const Rect b(newDirty.getBounds());
224 eglSetSwapRectangleANDROID(mDisplay, mSurface,
225 b.left, b.top, b.width(), b.height());
226 }
Jesse Hall01e29052013-02-19 16:13:35 -0800227 }
Mathias Agopiand8707032012-09-18 01:21:55 -0700228
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700229 mPageFlipCount++;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800230}
231
Dan Stoza71433162014-02-04 16:22:36 -0800232status_t DisplayDevice::beginFrame(bool mustRecompose) const {
233 return mDisplaySurface->beginFrame(mustRecompose);
Jesse Hall028dc8f2013-08-20 16:35:32 -0700234}
235
Dan Stoza9e56aa02015-11-02 13:00:03 -0800236status_t DisplayDevice::prepareFrame(HWComposer& hwc) {
237 status_t error = hwc.prepare(*this);
238 if (error != NO_ERROR) {
239 return error;
240 }
241
242 DisplaySurface::CompositionType compositionType;
243 bool hasClient = hwc.hasClientComposition(mHwcDisplayId);
244 bool hasDevice = hwc.hasDeviceComposition(mHwcDisplayId);
245 if (hasClient && hasDevice) {
246 compositionType = DisplaySurface::COMPOSITION_MIXED;
247 } else if (hasClient) {
248 compositionType = DisplaySurface::COMPOSITION_GLES;
249 } else if (hasDevice) {
250 compositionType = DisplaySurface::COMPOSITION_HWC;
251 } else {
252 // Nothing to do -- when turning the screen off we get a frame like
253 // this. Call it a HWC frame since we won't be doing any GLES work but
254 // will do a prepare/set cycle.
255 compositionType = DisplaySurface::COMPOSITION_HWC;
256 }
257 return mDisplaySurface->prepareFrame(compositionType);
258}
Jesse Hall38efe862013-04-06 23:12:29 -0700259
Mathias Agopianda27af92012-09-13 18:17:13 -0700260void DisplayDevice::swapBuffers(HWComposer& hwc) const {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800261 if (hwc.hasClientComposition(mHwcDisplayId)) {
Jesse Hall99c7dbb2013-03-14 14:29:29 -0700262 EGLBoolean success = eglSwapBuffers(mDisplay, mSurface);
263 if (!success) {
264 EGLint error = eglGetError();
265 if (error == EGL_CONTEXT_LOST ||
266 mType == DisplayDevice::DISPLAY_PRIMARY) {
267 LOG_ALWAYS_FATAL("eglSwapBuffers(%p, %p) failed with 0x%08x",
268 mDisplay, mSurface, error);
269 } else {
270 ALOGE("eglSwapBuffers(%p, %p) failed with 0x%08x",
271 mDisplay, mSurface, error);
Mathias Agopianda27af92012-09-13 18:17:13 -0700272 }
273 }
274 }
Mathias Agopian52e21482012-09-24 18:07:21 -0700275
Jesse Hall99c7dbb2013-03-14 14:29:29 -0700276 status_t result = mDisplaySurface->advanceFrame();
277 if (result != NO_ERROR) {
278 ALOGE("[%s] failed pushing new frame to HWC: %d",
279 mDisplayName.string(), result);
Mathias Agopian32341382012-09-25 19:16:28 -0700280 }
Mathias Agopianda27af92012-09-13 18:17:13 -0700281}
282
Dan Stoza9e56aa02015-11-02 13:00:03 -0800283void DisplayDevice::onSwapBuffersCompleted() const {
284 mDisplaySurface->onFrameCommitted();
285}
Mathias Agopianda27af92012-09-13 18:17:13 -0700286
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700287uint32_t DisplayDevice::getFlags() const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800288{
289 return mFlags;
290}
291
Mathias Agopian875d8e12013-06-07 15:35:48 -0700292EGLBoolean DisplayDevice::makeCurrent(EGLDisplay dpy, EGLContext ctx) const {
Mathias Agopianda8d0a52012-09-04 15:05:38 -0700293 EGLBoolean result = EGL_TRUE;
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700294 EGLSurface sur = eglGetCurrentSurface(EGL_DRAW);
Mathias Agopian875d8e12013-06-07 15:35:48 -0700295 if (sur != mSurface) {
296 result = eglMakeCurrent(dpy, mSurface, mSurface, ctx);
Mathias Agopianda8d0a52012-09-04 15:05:38 -0700297 if (result == EGL_TRUE) {
Jesse Hallf460f552013-08-06 17:08:53 -0700298 if (mType >= DisplayDevice::DISPLAY_VIRTUAL)
299 eglSwapInterval(dpy, 0);
Mathias Agopianda8d0a52012-09-04 15:05:38 -0700300 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700301 }
Mathias Agopian931bda12013-08-28 18:11:46 -0700302 setViewportAndProjection();
Mathias Agopianda8d0a52012-09-04 15:05:38 -0700303 return result;
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700304}
305
Mathias Agopian875d8e12013-06-07 15:35:48 -0700306void DisplayDevice::setViewportAndProjection() const {
307 size_t w = mDisplayWidth;
308 size_t h = mDisplayHeight;
Dan Stozac1879002014-05-22 15:59:05 -0700309 Rect sourceCrop(0, 0, w, h);
Riley Andrewsc3ebe662014-09-04 16:20:31 -0700310 mFlinger->getRenderEngine().setViewportAndProjection(w, h, sourceCrop, h,
311 false, Transform::ROT_0);
Mathias Agopianbae92d02012-09-28 01:00:47 -0700312}
313
Dan Stoza9e56aa02015-11-02 13:00:03 -0800314const sp<Fence>& DisplayDevice::getClientTargetAcquireFence() const {
315 return mDisplaySurface->getClientTargetAcquireFence();
316}
Dan Stoza9e56aa02015-11-02 13:00:03 -0800317
Mathias Agopian1b031492012-06-20 17:51:20 -0700318// ----------------------------------------------------------------------------
319
Mathias Agopian13127d82013-03-05 17:47:11 -0800320void DisplayDevice::setVisibleLayersSortedByZ(const Vector< sp<Layer> >& layers) {
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700321 mVisibleLayersSortedByZ = layers;
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700322}
323
Mathias Agopian13127d82013-03-05 17:47:11 -0800324const Vector< sp<Layer> >& DisplayDevice::getVisibleLayersSortedByZ() const {
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700325 return mVisibleLayersSortedByZ;
326}
327
Chia-I Wu83806892017-11-16 10:50:20 -0800328void DisplayDevice::setLayersNeedingFences(const Vector< sp<Layer> >& layers) {
329 mLayersNeedingFences = layers;
330}
331
332const Vector< sp<Layer> >& DisplayDevice::getLayersNeedingFences() const {
333 return mLayersNeedingFences;
334}
335
Mathias Agopiancd60f992012-08-16 16:28:27 -0700336Region DisplayDevice::getDirtyRegion(bool repaintEverything) const {
337 Region dirty;
Mathias Agopiancd60f992012-08-16 16:28:27 -0700338 if (repaintEverything) {
339 dirty.set(getBounds());
340 } else {
Mathias Agopianda8d0a52012-09-04 15:05:38 -0700341 const Transform& planeTransform(mGlobalTransform);
Mathias Agopiancd60f992012-08-16 16:28:27 -0700342 dirty = planeTransform.transform(this->dirtyRegion);
343 dirty.andSelf(getBounds());
344 }
345 return dirty;
346}
347
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700348// ----------------------------------------------------------------------------
Prashant Malani2c9b11f2014-05-25 01:36:31 -0700349void DisplayDevice::setPowerMode(int mode) {
350 mPowerMode = mode;
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700351}
352
Prashant Malani2c9b11f2014-05-25 01:36:31 -0700353int DisplayDevice::getPowerMode() const {
354 return mPowerMode;
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700355}
356
Prashant Malani2c9b11f2014-05-25 01:36:31 -0700357bool DisplayDevice::isDisplayOn() const {
358 return (mPowerMode != HWC_POWER_MODE_OFF);
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700359}
360
361// ----------------------------------------------------------------------------
Michael Lentine6c9e34a2014-07-14 13:48:55 -0700362void DisplayDevice::setActiveConfig(int mode) {
363 mActiveConfig = mode;
364}
365
366int DisplayDevice::getActiveConfig() const {
367 return mActiveConfig;
368}
369
370// ----------------------------------------------------------------------------
Michael Wright28f24d02016-07-12 13:30:53 -0700371void DisplayDevice::setActiveColorMode(android_color_mode_t mode) {
372 mActiveColorMode = mode;
373}
374
375android_color_mode_t DisplayDevice::getActiveColorMode() const {
376 return mActiveColorMode;
377}
Courtney Goeltzenleuchter79d27242017-07-13 17:54:01 -0600378
379void DisplayDevice::setCompositionDataSpace(android_dataspace dataspace) {
380 ANativeWindow* const window = mNativeWindow.get();
381 native_window_set_buffers_data_space(window, dataspace);
382}
Michael Wright28f24d02016-07-12 13:30:53 -0700383
384// ----------------------------------------------------------------------------
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700385
Mathias Agopian28947d72012-08-08 18:51:15 -0700386void DisplayDevice::setLayerStack(uint32_t stack) {
387 mLayerStack = stack;
388 dirtyRegion.set(bounds());
389}
390
391// ----------------------------------------------------------------------------
392
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700393uint32_t DisplayDevice::getOrientationTransform() const {
394 uint32_t transform = 0;
395 switch (mOrientation) {
396 case DisplayState::eOrientationDefault:
397 transform = Transform::ROT_0;
398 break;
399 case DisplayState::eOrientation90:
400 transform = Transform::ROT_90;
401 break;
402 case DisplayState::eOrientation180:
403 transform = Transform::ROT_180;
404 break;
405 case DisplayState::eOrientation270:
406 transform = Transform::ROT_270;
407 break;
408 }
409 return transform;
410}
411
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700412status_t DisplayDevice::orientationToTransfrom(
Mathias Agopian1b031492012-06-20 17:51:20 -0700413 int orientation, int w, int h, Transform* tr)
414{
415 uint32_t flags = 0;
416 switch (orientation) {
Mathias Agopian3165cc22012-08-08 19:42:09 -0700417 case DisplayState::eOrientationDefault:
Mathias Agopian1b031492012-06-20 17:51:20 -0700418 flags = Transform::ROT_0;
419 break;
Mathias Agopian3165cc22012-08-08 19:42:09 -0700420 case DisplayState::eOrientation90:
Mathias Agopian1b031492012-06-20 17:51:20 -0700421 flags = Transform::ROT_90;
422 break;
Mathias Agopian3165cc22012-08-08 19:42:09 -0700423 case DisplayState::eOrientation180:
Mathias Agopian1b031492012-06-20 17:51:20 -0700424 flags = Transform::ROT_180;
425 break;
Mathias Agopian3165cc22012-08-08 19:42:09 -0700426 case DisplayState::eOrientation270:
Mathias Agopian1b031492012-06-20 17:51:20 -0700427 flags = Transform::ROT_270;
428 break;
429 default:
430 return BAD_VALUE;
431 }
432 tr->set(flags, w, h);
433 return NO_ERROR;
434}
435
Michael Lentine47e45402014-07-18 15:34:25 -0700436void DisplayDevice::setDisplaySize(const int newWidth, const int newHeight) {
437 dirtyRegion.set(getBounds());
438
Michael Lentinef2568de2014-08-20 10:51:23 -0700439 if (mSurface != EGL_NO_SURFACE) {
440 eglDestroySurface(mDisplay, mSurface);
441 mSurface = EGL_NO_SURFACE;
442 }
443
Michael Lentine47e45402014-07-18 15:34:25 -0700444 mDisplaySurface->resizeBuffers(newWidth, newHeight);
445
446 ANativeWindow* const window = mNativeWindow.get();
447 mSurface = eglCreateWindowSurface(mDisplay, mConfig, window, NULL);
448 eglQuerySurface(mDisplay, mSurface, EGL_WIDTH, &mDisplayWidth);
449 eglQuerySurface(mDisplay, mSurface, EGL_HEIGHT, &mDisplayHeight);
450
451 LOG_FATAL_IF(mDisplayWidth != newWidth,
452 "Unable to set new width to %d", newWidth);
453 LOG_FATAL_IF(mDisplayHeight != newHeight,
454 "Unable to set new height to %d", newHeight);
455}
456
Mathias Agopian00e8c7a2012-09-04 19:30:46 -0700457void DisplayDevice::setProjection(int orientation,
Mathias Agopianf5f714a2013-02-26 16:54:05 -0800458 const Rect& newViewport, const Rect& newFrame) {
459 Rect viewport(newViewport);
460 Rect frame(newFrame);
461
462 const int w = mDisplayWidth;
463 const int h = mDisplayHeight;
464
465 Transform R;
466 DisplayDevice::orientationToTransfrom(orientation, w, h, &R);
467
468 if (!frame.isValid()) {
469 // the destination frame can be invalid if it has never been set,
470 // in that case we assume the whole display frame.
471 frame = Rect(w, h);
472 }
473
474 if (viewport.isEmpty()) {
475 // viewport can be invalid if it has never been set, in that case
476 // we assume the whole display size.
477 // it's also invalid to have an empty viewport, so we handle that
478 // case in the same way.
479 viewport = Rect(w, h);
480 if (R.getOrientation() & Transform::ROT_90) {
481 // viewport is always specified in the logical orientation
482 // of the display (ie: post-rotation).
483 swap(viewport.right, viewport.bottom);
484 }
485 }
486
487 dirtyRegion.set(getBounds());
488
489 Transform TL, TP, S;
490 float src_width = viewport.width();
491 float src_height = viewport.height();
492 float dst_width = frame.width();
493 float dst_height = frame.height();
494 if (src_width != dst_width || src_height != dst_height) {
495 float sx = dst_width / src_width;
496 float sy = dst_height / src_height;
497 S.set(sx, 0, 0, sy);
498 }
499
500 float src_x = viewport.left;
501 float src_y = viewport.top;
502 float dst_x = frame.left;
503 float dst_y = frame.top;
504 TL.set(-src_x, -src_y);
505 TP.set(dst_x, dst_y);
506
507 // The viewport and frame are both in the logical orientation.
508 // Apply the logical translation, scale to physical size, apply the
509 // physical translation and finally rotate to the physical orientation.
510 mGlobalTransform = R * TP * S * TL;
511
512 const uint8_t type = mGlobalTransform.getType();
513 mNeedsFiltering = (!mGlobalTransform.preserveRects() ||
514 (type >= Transform::SCALE));
515
516 mScissor = mGlobalTransform.transform(viewport);
517 if (mScissor.isEmpty()) {
Mathias Agopian6c7f25a2013-05-09 20:37:10 -0700518 mScissor = getBounds();
Mathias Agopianf5f714a2013-02-26 16:54:05 -0800519 }
520
Mathias Agopianda8d0a52012-09-04 15:05:38 -0700521 mOrientation = orientation;
Pablo Ceballos021623b2016-04-15 17:31:51 -0700522 if (mType == DisplayType::DISPLAY_PRIMARY) {
523 uint32_t transform = 0;
524 switch (mOrientation) {
525 case DisplayState::eOrientationDefault:
526 transform = Transform::ROT_0;
527 break;
528 case DisplayState::eOrientation90:
529 transform = Transform::ROT_90;
530 break;
531 case DisplayState::eOrientation180:
532 transform = Transform::ROT_180;
533 break;
534 case DisplayState::eOrientation270:
535 transform = Transform::ROT_270;
536 break;
537 }
538 sPrimaryDisplayOrientation = transform;
539 }
Mathias Agopian00e8c7a2012-09-04 19:30:46 -0700540 mViewport = viewport;
541 mFrame = frame;
Mathias Agopian1b031492012-06-20 17:51:20 -0700542}
Mathias Agopian1d12d8a2012-09-18 01:38:00 -0700543
Pablo Ceballos021623b2016-04-15 17:31:51 -0700544uint32_t DisplayDevice::getPrimaryDisplayOrientationTransform() {
545 return sPrimaryDisplayOrientation;
546}
547
Mathias Agopian74d211a2013-04-22 16:55:35 +0200548void DisplayDevice::dump(String8& result) const {
Mathias Agopian1d12d8a2012-09-18 01:38:00 -0700549 const Transform& tr(mGlobalTransform);
Courtney Goeltzenleuchter152279d2017-08-14 18:18:30 -0600550 ANativeWindow* const window = mNativeWindow.get();
Courtney Goeltzenleuchter0ebaac32017-04-13 12:17:03 -0600551 EGLint redSize, greenSize, blueSize, alphaSize;
552 eglGetConfigAttrib(mDisplay, mConfig, EGL_RED_SIZE, &redSize);
553 eglGetConfigAttrib(mDisplay, mConfig, EGL_GREEN_SIZE, &greenSize);
554 eglGetConfigAttrib(mDisplay, mConfig, EGL_BLUE_SIZE, &blueSize);
555 eglGetConfigAttrib(mDisplay, mConfig, EGL_ALPHA_SIZE, &alphaSize);
556 result.appendFormat("+ DisplayDevice: %s\n", mDisplayName.string());
557 result.appendFormat(" type=%x, hwcId=%d, layerStack=%u, (%4dx%4d), ANativeWindow=%p "
558 "(%d:%d:%d:%d), orient=%2d (type=%08x), "
559 "flips=%u, isSecure=%d, powerMode=%d, activeConfig=%d, numLayers=%zu\n",
Courtney Goeltzenleuchter152279d2017-08-14 18:18:30 -0600560 mType, mHwcDisplayId, mLayerStack, mDisplayWidth, mDisplayHeight, window,
561 redSize, greenSize, blueSize, alphaSize, mOrientation, tr.getType(),
562 getPageFlipCount(), mIsSecure, mPowerMode, mActiveConfig,
Courtney Goeltzenleuchter0ebaac32017-04-13 12:17:03 -0600563 mVisibleLayersSortedByZ.size());
564 result.appendFormat(" v:[%d,%d,%d,%d], f:[%d,%d,%d,%d], s:[%d,%d,%d,%d],"
565 "transform:[[%0.3f,%0.3f,%0.3f][%0.3f,%0.3f,%0.3f][%0.3f,%0.3f,%0.3f]]\n",
566 mViewport.left, mViewport.top, mViewport.right, mViewport.bottom,
567 mFrame.left, mFrame.top, mFrame.right, mFrame.bottom, mScissor.left,
568 mScissor.top, mScissor.right, mScissor.bottom, tr[0][0], tr[1][0], tr[2][0],
569 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 -0600570 auto const surface = static_cast<Surface*>(window);
571 android_dataspace dataspace = surface->getBuffersDataSpace();
572 result.appendFormat(" dataspace: %s (%d)\n", dataspaceDetails(dataspace).c_str(), dataspace);
Mathias Agopian1d12d8a2012-09-18 01:38:00 -0700573
Jesse Hall99c7dbb2013-03-14 14:29:29 -0700574 String8 surfaceDump;
Dan Stozaf10c46e2014-11-11 10:32:31 -0800575 mDisplaySurface->dumpAsString(surfaceDump);
Jesse Hall99c7dbb2013-03-14 14:29:29 -0700576 result.append(surfaceDump);
Mathias Agopian1d12d8a2012-09-18 01:38:00 -0700577}
Irvelffc9efc2016-07-27 15:16:37 -0700578
579std::atomic<int32_t> DisplayDeviceState::nextDisplayId(1);
580
581DisplayDeviceState::DisplayDeviceState(DisplayDevice::DisplayType type, bool isSecure)
582 : type(type),
583 layerStack(DisplayDevice::NO_LAYER_STACK),
584 orientation(0),
585 width(0),
586 height(0),
587 isSecure(isSecure)
588{
589 viewport.makeInvalid();
590 frame.makeInvalid();
591}