blob: 0244c1b862758ab49f523b445af21effd3689798 [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"
Fabien Sanglard9d96de42016-10-11 00:15:18 +000041#ifdef USE_HWC2
Dan Stoza9e56aa02015-11-02 13:00:03 -080042#include "DisplayHardware/HWC2.h"
Fabien Sanglard9d96de42016-10-11 00:15:18 +000043#endif
Mathias Agopian875d8e12013-06-07 15:35:48 -070044#include "RenderEngine/RenderEngine.h"
Mathias Agopian1b031492012-06-20 17:51:20 -070045
Mathias Agopianda8d0a52012-09-04 15:05:38 -070046#include "clz.h"
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -070047#include "DisplayDevice.h"
Mathias Agopianc7d14e22011-08-01 16:32:21 -070048#include "SurfaceFlinger.h"
Mathias Agopian13127d82013-03-05 17:47:11 -080049#include "Layer.h"
Mathias Agopian1f7bec62010-06-25 18:02:21 -070050
Jaesoo Lee720a7242017-01-31 15:26:18 +090051#include <android/hardware/configstore/1.0/ISurfaceFlingerConfigs.h>
52#include <configstore/Utils.h>
53
Mathias Agopiana4912602012-07-12 14:25:33 -070054// ----------------------------------------------------------------------------
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080055using namespace android;
Mathias Agopiana4912602012-07-12 14:25:33 -070056// ----------------------------------------------------------------------------
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080057
Andreas Gampe89fd4f72014-11-13 14:18:56 -080058#ifdef EGL_ANDROID_swap_rectangle
59static constexpr bool kEGLAndroidSwapRectangle = true;
60#else
61static constexpr bool kEGLAndroidSwapRectangle = false;
62#endif
63
Jaesoo Lee720a7242017-01-31 15:26:18 +090064// retrieve triple buffer setting from configstore
65using namespace android::hardware::configstore;
66using namespace android::hardware::configstore::V1_0;
67
Fabien Sanglard1971b632017-03-10 14:50:03 -080068static bool useTripleFramebuffer = getInt64< ISurfaceFlingerConfigs,
Fabien Sanglard48eb5652017-07-26 14:55:11 -070069 &ISurfaceFlingerConfigs::maxFrameBufferAcquiredBuffers>(2) >= 3;
Jaesoo Lee720a7242017-01-31 15:26:18 +090070
Andreas Gampe89fd4f72014-11-13 14:18:56 -080071#if !defined(EGL_EGLEXT_PROTOTYPES) || !defined(EGL_ANDROID_swap_rectangle)
72// Dummy implementation in case it is missing.
73inline void eglSetSwapRectangleANDROID (EGLDisplay, EGLSurface, EGLint, EGLint, EGLint, EGLint) {
74}
75#endif
76
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080077/*
78 * Initialize the display to the specified values.
79 *
80 */
81
Pablo Ceballos021623b2016-04-15 17:31:51 -070082uint32_t DisplayDevice::sPrimaryDisplayOrientation = 0;
83
Courtney Goeltzenleuchter5d943892017-03-22 13:46:46 -060084// clang-format off
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -070085DisplayDevice::DisplayDevice(
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080086 const sp<SurfaceFlinger>& flinger,
Jamie Gennisdd3cb842012-10-19 18:19:11 -070087 DisplayType type,
Jesse Hallffe1f192013-03-22 15:13:48 -070088 int32_t hwcId,
Fabien Sanglard9d96de42016-10-11 00:15:18 +000089#ifndef USE_HWC2
90 int format,
91#endif
Jamie Gennisdd3cb842012-10-19 18:19:11 -070092 bool isSecure,
93 const wp<IBinder>& displayToken,
Jesse Hall99c7dbb2013-03-14 14:29:29 -070094 const sp<DisplaySurface>& displaySurface,
Mathias Agopiandb89edc2013-08-02 01:40:18 -070095 const sp<IGraphicBufferProducer>& producer,
Courtney Goeltzenleuchter5d943892017-03-22 13:46:46 -060096 EGLConfig config,
97 bool supportWideColor)
Jesse Hallb7a05492014-08-14 15:45:06 -070098 : lastCompositionHadVisibleLayers(false),
99 mFlinger(flinger),
Dan Stoza9e56aa02015-11-02 13:00:03 -0800100 mType(type),
101 mHwcDisplayId(hwcId),
Chih-Wei Huang27e25622013-01-07 17:33:56 +0800102 mDisplayToken(displayToken),
Jesse Hall99c7dbb2013-03-14 14:29:29 -0700103 mDisplaySurface(displaySurface),
Mathias Agopian92a979a2012-08-02 18:32:23 -0700104 mDisplay(EGL_NO_DISPLAY),
105 mSurface(EGL_NO_SURFACE),
Dan Stoza9e56aa02015-11-02 13:00:03 -0800106 mDisplayWidth(),
107 mDisplayHeight(),
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000108#ifndef USE_HWC2
109 mFormat(),
110#endif
Mathias Agopian92a979a2012-08-02 18:32:23 -0700111 mFlags(),
112 mPageFlipCount(),
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700113 mIsSecure(isSecure),
Jesse Hall01e29052013-02-19 16:13:35 -0800114 mLayerStack(NO_LAYER_STACK),
Prashant Malani2c9b11f2014-05-25 01:36:31 -0700115 mOrientation(),
Michael Lentine6c9e34a2014-07-14 13:48:55 -0700116 mPowerMode(HWC_POWER_MODE_OFF),
Courtney Goeltzenleuchter4f20f9c2017-04-06 08:18:34 -0600117 mActiveConfig(0)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800118{
Courtney Goeltzenleuchter5d943892017-03-22 13:46:46 -0600119 // clang-format on
james.zhang5e8eb5e2015-12-01 17:55:11 +0800120 Surface* surface;
121 mNativeWindow = surface = new Surface(producer, false);
Jesse Hallffe1f192013-03-22 15:13:48 -0700122 ANativeWindow* const window = mNativeWindow.get();
123
Courtney Goeltzenleuchter62caf7c2017-03-14 14:18:28 -0600124#ifdef USE_HWC2
Naseer Ahmed901a86e2017-05-11 11:15:21 -0400125 mActiveColorMode = HAL_COLOR_MODE_NATIVE;
Courtney Goeltzenleuchter4f20f9c2017-04-06 08:18:34 -0600126 mDisplayHasWideColor = supportWideColor;
127#else
128 (void) supportWideColor;
Courtney Goeltzenleuchter62caf7c2017-03-14 14:18:28 -0600129#endif
Jesse Hallffe1f192013-03-22 15:13:48 -0700130 /*
131 * Create our display's surface
132 */
133
james.zhang5e8eb5e2015-12-01 17:55:11 +0800134 EGLSurface eglSurface;
Jesse Hallffe1f192013-03-22 15:13:48 -0700135 EGLDisplay display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
Jesse Hall19e87292013-12-23 21:02:15 -0800136 if (config == EGL_NO_CONFIG) {
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000137#ifdef USE_HWC2
Steven Thomas94e35b92017-07-26 18:48:28 -0700138 config = RenderEngine::chooseEglConfig(display, PIXEL_FORMAT_RGBA_8888,
139 /*logConfig*/ false);
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000140#else
Steven Thomas94e35b92017-07-26 18:48:28 -0700141 config = RenderEngine::chooseEglConfig(display, format,
142 /*logConfig*/ false);
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000143#endif
Jesse Hall19e87292013-12-23 21:02:15 -0800144 }
james.zhang5e8eb5e2015-12-01 17:55:11 +0800145 eglSurface = eglCreateWindowSurface(display, config, window, NULL);
146 eglQuerySurface(display, eglSurface, EGL_WIDTH, &mDisplayWidth);
147 eglQuerySurface(display, eglSurface, EGL_HEIGHT, &mDisplayHeight);
Jesse Hallffe1f192013-03-22 15:13:48 -0700148
John Dong4ee56962014-02-21 12:37:59 -0800149 // Make sure that composition can never be stalled by a virtual display
150 // consumer that isn't processing buffers fast enough. We have to do this
151 // in two places:
152 // * Here, in case the display is composed entirely by HWC.
153 // * In makeCurrent(), using eglSwapInterval. Some EGL drivers set the
154 // window's swap interval in eglMakeCurrent, so they'll override the
155 // interval we set here.
156 if (mType >= DisplayDevice::DISPLAY_VIRTUAL)
157 window->setSwapInterval(window, 0);
158
Michael Lentine47e45402014-07-18 15:34:25 -0700159 mConfig = config;
Jesse Hallffe1f192013-03-22 15:13:48 -0700160 mDisplay = display;
james.zhang5e8eb5e2015-12-01 17:55:11 +0800161 mSurface = eglSurface;
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000162#ifndef USE_HWC2
163 mFormat = format;
164#endif
Jesse Hallffe1f192013-03-22 15:13:48 -0700165 mPageFlipCount = 0;
166 mViewport.makeInvalid();
167 mFrame.makeInvalid();
168
169 // virtual displays are always considered enabled
Prashant Malani2c9b11f2014-05-25 01:36:31 -0700170 mPowerMode = (mType >= DisplayDevice::DISPLAY_VIRTUAL) ?
171 HWC_POWER_MODE_NORMAL : HWC_POWER_MODE_OFF;
Jesse Hallffe1f192013-03-22 15:13:48 -0700172
173 // Name the display. The name will be replaced shortly if the display
174 // was created with createDisplay().
175 switch (mType) {
176 case DISPLAY_PRIMARY:
177 mDisplayName = "Built-in Screen";
178 break;
179 case DISPLAY_EXTERNAL:
180 mDisplayName = "HDMI Screen";
181 break;
182 default:
183 mDisplayName = "Virtual Screen"; // e.g. Overlay #n
184 break;
185 }
186
187 // initialize the display orientation transform.
188 setProjection(DisplayState::eOrientationDefault, mViewport, mFrame);
james.zhang5e8eb5e2015-12-01 17:55:11 +0800189
Jaesoo Lee720a7242017-01-31 15:26:18 +0900190 if (useTripleFramebuffer) {
191 surface->allocateBuffers();
192 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800193}
194
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700195DisplayDevice::~DisplayDevice() {
Mathias Agopian92a979a2012-08-02 18:32:23 -0700196 if (mSurface != EGL_NO_SURFACE) {
197 eglDestroySurface(mDisplay, mSurface);
198 mSurface = EGL_NO_SURFACE;
199 }
200}
201
Jesse Hall02d86562013-03-25 14:43:23 -0700202void DisplayDevice::disconnect(HWComposer& hwc) {
203 if (mHwcDisplayId >= 0) {
204 hwc.disconnectDisplay(mHwcDisplayId);
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000205#ifndef USE_HWC2
206 if (mHwcDisplayId >= DISPLAY_VIRTUAL)
207 hwc.freeDisplayId(mHwcDisplayId);
208#endif
Jesse Hall02d86562013-03-25 14:43:23 -0700209 mHwcDisplayId = -1;
210 }
211}
212
Mathias Agopian92a979a2012-08-02 18:32:23 -0700213bool DisplayDevice::isValid() const {
214 return mFlinger != NULL;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800215}
216
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700217int DisplayDevice::getWidth() const {
Mathias Agopiana4912602012-07-12 14:25:33 -0700218 return mDisplayWidth;
219}
220
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700221int DisplayDevice::getHeight() const {
Mathias Agopiana4912602012-07-12 14:25:33 -0700222 return mDisplayHeight;
223}
224
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000225#ifndef USE_HWC2
226PixelFormat DisplayDevice::getFormat() const {
227 return mFormat;
228}
229#endif
230
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700231EGLSurface DisplayDevice::getEGLSurface() const {
Mathias Agopiana4912602012-07-12 14:25:33 -0700232 return mSurface;
233}
234
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700235void DisplayDevice::setDisplayName(const String8& displayName) {
236 if (!displayName.isEmpty()) {
237 // never override the name with an empty name
238 mDisplayName = displayName;
239 }
240}
241
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700242uint32_t DisplayDevice::getPageFlipCount() const {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700243 return mPageFlipCount;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800244}
245
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000246#ifndef USE_HWC2
247status_t DisplayDevice::compositionComplete() const {
248 return mDisplaySurface->compositionComplete();
249}
250#endif
251
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700252void DisplayDevice::flip(const Region& dirty) const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800253{
Mathias Agopian875d8e12013-06-07 15:35:48 -0700254 mFlinger->getRenderEngine().checkErrors();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800255
Andreas Gampe89fd4f72014-11-13 14:18:56 -0800256 if (kEGLAndroidSwapRectangle) {
257 if (mFlags & SWAP_RECTANGLE) {
258 const Region newDirty(dirty.intersect(bounds()));
259 const Rect b(newDirty.getBounds());
260 eglSetSwapRectangleANDROID(mDisplay, mSurface,
261 b.left, b.top, b.width(), b.height());
262 }
Jesse Hall01e29052013-02-19 16:13:35 -0800263 }
Mathias Agopiand8707032012-09-18 01:21:55 -0700264
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700265 mPageFlipCount++;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800266}
267
Dan Stoza71433162014-02-04 16:22:36 -0800268status_t DisplayDevice::beginFrame(bool mustRecompose) const {
269 return mDisplaySurface->beginFrame(mustRecompose);
Jesse Hall028dc8f2013-08-20 16:35:32 -0700270}
271
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000272#ifdef USE_HWC2
Dan Stoza9e56aa02015-11-02 13:00:03 -0800273status_t DisplayDevice::prepareFrame(HWComposer& hwc) {
274 status_t error = hwc.prepare(*this);
275 if (error != NO_ERROR) {
276 return error;
277 }
278
279 DisplaySurface::CompositionType compositionType;
280 bool hasClient = hwc.hasClientComposition(mHwcDisplayId);
281 bool hasDevice = hwc.hasDeviceComposition(mHwcDisplayId);
282 if (hasClient && hasDevice) {
283 compositionType = DisplaySurface::COMPOSITION_MIXED;
284 } else if (hasClient) {
285 compositionType = DisplaySurface::COMPOSITION_GLES;
286 } else if (hasDevice) {
287 compositionType = DisplaySurface::COMPOSITION_HWC;
288 } else {
289 // Nothing to do -- when turning the screen off we get a frame like
290 // this. Call it a HWC frame since we won't be doing any GLES work but
291 // will do a prepare/set cycle.
292 compositionType = DisplaySurface::COMPOSITION_HWC;
293 }
294 return mDisplaySurface->prepareFrame(compositionType);
295}
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000296#else
297status_t DisplayDevice::prepareFrame(const HWComposer& hwc) const {
298 DisplaySurface::CompositionType compositionType;
299 bool haveGles = hwc.hasGlesComposition(mHwcDisplayId);
300 bool haveHwc = hwc.hasHwcComposition(mHwcDisplayId);
301 if (haveGles && haveHwc) {
302 compositionType = DisplaySurface::COMPOSITION_MIXED;
303 } else if (haveGles) {
304 compositionType = DisplaySurface::COMPOSITION_GLES;
305 } else if (haveHwc) {
306 compositionType = DisplaySurface::COMPOSITION_HWC;
307 } else {
308 // Nothing to do -- when turning the screen off we get a frame like
309 // this. Call it a HWC frame since we won't be doing any GLES work but
310 // will do a prepare/set cycle.
311 compositionType = DisplaySurface::COMPOSITION_HWC;
312 }
313 return mDisplaySurface->prepareFrame(compositionType);
314}
315#endif
Jesse Hall38efe862013-04-06 23:12:29 -0700316
Mathias Agopianda27af92012-09-13 18:17:13 -0700317void DisplayDevice::swapBuffers(HWComposer& hwc) const {
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000318#ifdef USE_HWC2
Dan Stoza9e56aa02015-11-02 13:00:03 -0800319 if (hwc.hasClientComposition(mHwcDisplayId)) {
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000320#else
321 // We need to call eglSwapBuffers() if:
322 // (1) we don't have a hardware composer, or
323 // (2) we did GLES composition this frame, and either
324 // (a) we have framebuffer target support (not present on legacy
325 // devices, where HWComposer::commit() handles things); or
326 // (b) this is a virtual display
327 if (hwc.initCheck() != NO_ERROR ||
328 (hwc.hasGlesComposition(mHwcDisplayId) &&
329 (hwc.supportsFramebufferTarget() || mType >= DISPLAY_VIRTUAL))) {
330#endif
Jesse Hall99c7dbb2013-03-14 14:29:29 -0700331 EGLBoolean success = eglSwapBuffers(mDisplay, mSurface);
332 if (!success) {
333 EGLint error = eglGetError();
334 if (error == EGL_CONTEXT_LOST ||
335 mType == DisplayDevice::DISPLAY_PRIMARY) {
336 LOG_ALWAYS_FATAL("eglSwapBuffers(%p, %p) failed with 0x%08x",
337 mDisplay, mSurface, error);
338 } else {
339 ALOGE("eglSwapBuffers(%p, %p) failed with 0x%08x",
340 mDisplay, mSurface, error);
Mathias Agopianda27af92012-09-13 18:17:13 -0700341 }
342 }
343 }
Mathias Agopian52e21482012-09-24 18:07:21 -0700344
Jesse Hall99c7dbb2013-03-14 14:29:29 -0700345 status_t result = mDisplaySurface->advanceFrame();
346 if (result != NO_ERROR) {
347 ALOGE("[%s] failed pushing new frame to HWC: %d",
348 mDisplayName.string(), result);
Mathias Agopian32341382012-09-25 19:16:28 -0700349 }
Mathias Agopianda27af92012-09-13 18:17:13 -0700350}
351
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000352#ifdef USE_HWC2
Dan Stoza9e56aa02015-11-02 13:00:03 -0800353void DisplayDevice::onSwapBuffersCompleted() const {
354 mDisplaySurface->onFrameCommitted();
355}
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000356#else
357void DisplayDevice::onSwapBuffersCompleted(HWComposer& hwc) const {
358 if (hwc.initCheck() == NO_ERROR) {
359 mDisplaySurface->onFrameCommitted();
360 }
361}
362#endif
Mathias Agopianda27af92012-09-13 18:17:13 -0700363
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700364uint32_t DisplayDevice::getFlags() const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800365{
366 return mFlags;
367}
368
Mathias Agopian875d8e12013-06-07 15:35:48 -0700369EGLBoolean DisplayDevice::makeCurrent(EGLDisplay dpy, EGLContext ctx) const {
Mathias Agopianda8d0a52012-09-04 15:05:38 -0700370 EGLBoolean result = EGL_TRUE;
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700371 EGLSurface sur = eglGetCurrentSurface(EGL_DRAW);
Mathias Agopian875d8e12013-06-07 15:35:48 -0700372 if (sur != mSurface) {
373 result = eglMakeCurrent(dpy, mSurface, mSurface, ctx);
Mathias Agopianda8d0a52012-09-04 15:05:38 -0700374 if (result == EGL_TRUE) {
Jesse Hallf460f552013-08-06 17:08:53 -0700375 if (mType >= DisplayDevice::DISPLAY_VIRTUAL)
376 eglSwapInterval(dpy, 0);
Mathias Agopianda8d0a52012-09-04 15:05:38 -0700377 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700378 }
Mathias Agopian931bda12013-08-28 18:11:46 -0700379 setViewportAndProjection();
Mathias Agopianda8d0a52012-09-04 15:05:38 -0700380 return result;
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700381}
382
Mathias Agopian875d8e12013-06-07 15:35:48 -0700383void DisplayDevice::setViewportAndProjection() const {
384 size_t w = mDisplayWidth;
385 size_t h = mDisplayHeight;
Dan Stozac1879002014-05-22 15:59:05 -0700386 Rect sourceCrop(0, 0, w, h);
Riley Andrewsc3ebe662014-09-04 16:20:31 -0700387 mFlinger->getRenderEngine().setViewportAndProjection(w, h, sourceCrop, h,
388 false, Transform::ROT_0);
Mathias Agopianbae92d02012-09-28 01:00:47 -0700389}
390
Dan Stoza9e56aa02015-11-02 13:00:03 -0800391const sp<Fence>& DisplayDevice::getClientTargetAcquireFence() const {
392 return mDisplaySurface->getClientTargetAcquireFence();
393}
Dan Stoza9e56aa02015-11-02 13:00:03 -0800394
Mathias Agopian1b031492012-06-20 17:51:20 -0700395// ----------------------------------------------------------------------------
396
Mathias Agopian13127d82013-03-05 17:47:11 -0800397void DisplayDevice::setVisibleLayersSortedByZ(const Vector< sp<Layer> >& layers) {
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700398 mVisibleLayersSortedByZ = layers;
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700399}
400
Mathias Agopian13127d82013-03-05 17:47:11 -0800401const Vector< sp<Layer> >& DisplayDevice::getVisibleLayersSortedByZ() const {
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700402 return mVisibleLayersSortedByZ;
403}
404
Mathias Agopiancd60f992012-08-16 16:28:27 -0700405Region DisplayDevice::getDirtyRegion(bool repaintEverything) const {
406 Region dirty;
Mathias Agopiancd60f992012-08-16 16:28:27 -0700407 if (repaintEverything) {
408 dirty.set(getBounds());
409 } else {
Mathias Agopianda8d0a52012-09-04 15:05:38 -0700410 const Transform& planeTransform(mGlobalTransform);
Mathias Agopiancd60f992012-08-16 16:28:27 -0700411 dirty = planeTransform.transform(this->dirtyRegion);
412 dirty.andSelf(getBounds());
413 }
414 return dirty;
415}
416
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700417// ----------------------------------------------------------------------------
Prashant Malani2c9b11f2014-05-25 01:36:31 -0700418void DisplayDevice::setPowerMode(int mode) {
419 mPowerMode = mode;
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700420}
421
Prashant Malani2c9b11f2014-05-25 01:36:31 -0700422int DisplayDevice::getPowerMode() const {
423 return mPowerMode;
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700424}
425
Prashant Malani2c9b11f2014-05-25 01:36:31 -0700426bool DisplayDevice::isDisplayOn() const {
427 return (mPowerMode != HWC_POWER_MODE_OFF);
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700428}
429
430// ----------------------------------------------------------------------------
Michael Lentine6c9e34a2014-07-14 13:48:55 -0700431void DisplayDevice::setActiveConfig(int mode) {
432 mActiveConfig = mode;
433}
434
435int DisplayDevice::getActiveConfig() const {
436 return mActiveConfig;
437}
438
439// ----------------------------------------------------------------------------
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000440#ifdef USE_HWC2
Michael Wright28f24d02016-07-12 13:30:53 -0700441void DisplayDevice::setActiveColorMode(android_color_mode_t mode) {
442 mActiveColorMode = mode;
443}
444
445android_color_mode_t DisplayDevice::getActiveColorMode() const {
446 return mActiveColorMode;
447}
Courtney Goeltzenleuchter79d27242017-07-13 17:54:01 -0600448
449void DisplayDevice::setCompositionDataSpace(android_dataspace dataspace) {
450 ANativeWindow* const window = mNativeWindow.get();
451 native_window_set_buffers_data_space(window, dataspace);
452}
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000453#endif
Michael Wright28f24d02016-07-12 13:30:53 -0700454
455// ----------------------------------------------------------------------------
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700456
Mathias Agopian28947d72012-08-08 18:51:15 -0700457void DisplayDevice::setLayerStack(uint32_t stack) {
458 mLayerStack = stack;
459 dirtyRegion.set(bounds());
460}
461
462// ----------------------------------------------------------------------------
463
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700464uint32_t DisplayDevice::getOrientationTransform() const {
465 uint32_t transform = 0;
466 switch (mOrientation) {
467 case DisplayState::eOrientationDefault:
468 transform = Transform::ROT_0;
469 break;
470 case DisplayState::eOrientation90:
471 transform = Transform::ROT_90;
472 break;
473 case DisplayState::eOrientation180:
474 transform = Transform::ROT_180;
475 break;
476 case DisplayState::eOrientation270:
477 transform = Transform::ROT_270;
478 break;
479 }
480 return transform;
481}
482
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700483status_t DisplayDevice::orientationToTransfrom(
Mathias Agopian1b031492012-06-20 17:51:20 -0700484 int orientation, int w, int h, Transform* tr)
485{
486 uint32_t flags = 0;
487 switch (orientation) {
Mathias Agopian3165cc22012-08-08 19:42:09 -0700488 case DisplayState::eOrientationDefault:
Mathias Agopian1b031492012-06-20 17:51:20 -0700489 flags = Transform::ROT_0;
490 break;
Mathias Agopian3165cc22012-08-08 19:42:09 -0700491 case DisplayState::eOrientation90:
Mathias Agopian1b031492012-06-20 17:51:20 -0700492 flags = Transform::ROT_90;
493 break;
Mathias Agopian3165cc22012-08-08 19:42:09 -0700494 case DisplayState::eOrientation180:
Mathias Agopian1b031492012-06-20 17:51:20 -0700495 flags = Transform::ROT_180;
496 break;
Mathias Agopian3165cc22012-08-08 19:42:09 -0700497 case DisplayState::eOrientation270:
Mathias Agopian1b031492012-06-20 17:51:20 -0700498 flags = Transform::ROT_270;
499 break;
500 default:
501 return BAD_VALUE;
502 }
503 tr->set(flags, w, h);
504 return NO_ERROR;
505}
506
Michael Lentine47e45402014-07-18 15:34:25 -0700507void DisplayDevice::setDisplaySize(const int newWidth, const int newHeight) {
508 dirtyRegion.set(getBounds());
509
Michael Lentinef2568de2014-08-20 10:51:23 -0700510 if (mSurface != EGL_NO_SURFACE) {
511 eglDestroySurface(mDisplay, mSurface);
512 mSurface = EGL_NO_SURFACE;
513 }
514
Michael Lentine47e45402014-07-18 15:34:25 -0700515 mDisplaySurface->resizeBuffers(newWidth, newHeight);
516
517 ANativeWindow* const window = mNativeWindow.get();
518 mSurface = eglCreateWindowSurface(mDisplay, mConfig, window, NULL);
519 eglQuerySurface(mDisplay, mSurface, EGL_WIDTH, &mDisplayWidth);
520 eglQuerySurface(mDisplay, mSurface, EGL_HEIGHT, &mDisplayHeight);
521
522 LOG_FATAL_IF(mDisplayWidth != newWidth,
523 "Unable to set new width to %d", newWidth);
524 LOG_FATAL_IF(mDisplayHeight != newHeight,
525 "Unable to set new height to %d", newHeight);
526}
527
Mathias Agopian00e8c7a2012-09-04 19:30:46 -0700528void DisplayDevice::setProjection(int orientation,
Mathias Agopianf5f714a2013-02-26 16:54:05 -0800529 const Rect& newViewport, const Rect& newFrame) {
530 Rect viewport(newViewport);
531 Rect frame(newFrame);
532
533 const int w = mDisplayWidth;
534 const int h = mDisplayHeight;
535
536 Transform R;
537 DisplayDevice::orientationToTransfrom(orientation, w, h, &R);
538
539 if (!frame.isValid()) {
540 // the destination frame can be invalid if it has never been set,
541 // in that case we assume the whole display frame.
542 frame = Rect(w, h);
543 }
544
545 if (viewport.isEmpty()) {
546 // viewport can be invalid if it has never been set, in that case
547 // we assume the whole display size.
548 // it's also invalid to have an empty viewport, so we handle that
549 // case in the same way.
550 viewport = Rect(w, h);
551 if (R.getOrientation() & Transform::ROT_90) {
552 // viewport is always specified in the logical orientation
553 // of the display (ie: post-rotation).
554 swap(viewport.right, viewport.bottom);
555 }
556 }
557
558 dirtyRegion.set(getBounds());
559
560 Transform TL, TP, S;
561 float src_width = viewport.width();
562 float src_height = viewport.height();
563 float dst_width = frame.width();
564 float dst_height = frame.height();
565 if (src_width != dst_width || src_height != dst_height) {
566 float sx = dst_width / src_width;
567 float sy = dst_height / src_height;
568 S.set(sx, 0, 0, sy);
569 }
570
571 float src_x = viewport.left;
572 float src_y = viewport.top;
573 float dst_x = frame.left;
574 float dst_y = frame.top;
575 TL.set(-src_x, -src_y);
576 TP.set(dst_x, dst_y);
577
578 // The viewport and frame are both in the logical orientation.
579 // Apply the logical translation, scale to physical size, apply the
580 // physical translation and finally rotate to the physical orientation.
581 mGlobalTransform = R * TP * S * TL;
582
583 const uint8_t type = mGlobalTransform.getType();
584 mNeedsFiltering = (!mGlobalTransform.preserveRects() ||
585 (type >= Transform::SCALE));
586
587 mScissor = mGlobalTransform.transform(viewport);
588 if (mScissor.isEmpty()) {
Mathias Agopian6c7f25a2013-05-09 20:37:10 -0700589 mScissor = getBounds();
Mathias Agopianf5f714a2013-02-26 16:54:05 -0800590 }
591
Mathias Agopianda8d0a52012-09-04 15:05:38 -0700592 mOrientation = orientation;
Pablo Ceballos021623b2016-04-15 17:31:51 -0700593 if (mType == DisplayType::DISPLAY_PRIMARY) {
594 uint32_t transform = 0;
595 switch (mOrientation) {
596 case DisplayState::eOrientationDefault:
597 transform = Transform::ROT_0;
598 break;
599 case DisplayState::eOrientation90:
600 transform = Transform::ROT_90;
601 break;
602 case DisplayState::eOrientation180:
603 transform = Transform::ROT_180;
604 break;
605 case DisplayState::eOrientation270:
606 transform = Transform::ROT_270;
607 break;
608 }
609 sPrimaryDisplayOrientation = transform;
610 }
Mathias Agopian00e8c7a2012-09-04 19:30:46 -0700611 mViewport = viewport;
612 mFrame = frame;
Mathias Agopian1b031492012-06-20 17:51:20 -0700613}
Mathias Agopian1d12d8a2012-09-18 01:38:00 -0700614
Pablo Ceballos021623b2016-04-15 17:31:51 -0700615uint32_t DisplayDevice::getPrimaryDisplayOrientationTransform() {
616 return sPrimaryDisplayOrientation;
617}
618
Mathias Agopian74d211a2013-04-22 16:55:35 +0200619void DisplayDevice::dump(String8& result) const {
Mathias Agopian1d12d8a2012-09-18 01:38:00 -0700620 const Transform& tr(mGlobalTransform);
Courtney Goeltzenleuchter152279d2017-08-14 18:18:30 -0600621 ANativeWindow* const window = mNativeWindow.get();
Courtney Goeltzenleuchter0ebaac32017-04-13 12:17:03 -0600622 EGLint redSize, greenSize, blueSize, alphaSize;
623 eglGetConfigAttrib(mDisplay, mConfig, EGL_RED_SIZE, &redSize);
624 eglGetConfigAttrib(mDisplay, mConfig, EGL_GREEN_SIZE, &greenSize);
625 eglGetConfigAttrib(mDisplay, mConfig, EGL_BLUE_SIZE, &blueSize);
626 eglGetConfigAttrib(mDisplay, mConfig, EGL_ALPHA_SIZE, &alphaSize);
627 result.appendFormat("+ DisplayDevice: %s\n", mDisplayName.string());
628 result.appendFormat(" type=%x, hwcId=%d, layerStack=%u, (%4dx%4d), ANativeWindow=%p "
629 "(%d:%d:%d:%d), orient=%2d (type=%08x), "
630 "flips=%u, isSecure=%d, powerMode=%d, activeConfig=%d, numLayers=%zu\n",
Courtney Goeltzenleuchter152279d2017-08-14 18:18:30 -0600631 mType, mHwcDisplayId, mLayerStack, mDisplayWidth, mDisplayHeight, window,
632 redSize, greenSize, blueSize, alphaSize, mOrientation, tr.getType(),
633 getPageFlipCount(), mIsSecure, mPowerMode, mActiveConfig,
Courtney Goeltzenleuchter0ebaac32017-04-13 12:17:03 -0600634 mVisibleLayersSortedByZ.size());
635 result.appendFormat(" v:[%d,%d,%d,%d], f:[%d,%d,%d,%d], s:[%d,%d,%d,%d],"
636 "transform:[[%0.3f,%0.3f,%0.3f][%0.3f,%0.3f,%0.3f][%0.3f,%0.3f,%0.3f]]\n",
637 mViewport.left, mViewport.top, mViewport.right, mViewport.bottom,
638 mFrame.left, mFrame.top, mFrame.right, mFrame.bottom, mScissor.left,
639 mScissor.top, mScissor.right, mScissor.bottom, tr[0][0], tr[1][0], tr[2][0],
640 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 -0600641 auto const surface = static_cast<Surface*>(window);
642 android_dataspace dataspace = surface->getBuffersDataSpace();
643 result.appendFormat(" dataspace: %s (%d)\n", dataspaceDetails(dataspace).c_str(), dataspace);
Mathias Agopian1d12d8a2012-09-18 01:38:00 -0700644
Jesse Hall99c7dbb2013-03-14 14:29:29 -0700645 String8 surfaceDump;
Dan Stozaf10c46e2014-11-11 10:32:31 -0800646 mDisplaySurface->dumpAsString(surfaceDump);
Jesse Hall99c7dbb2013-03-14 14:29:29 -0700647 result.append(surfaceDump);
Mathias Agopian1d12d8a2012-09-18 01:38:00 -0700648}
Irvelffc9efc2016-07-27 15:16:37 -0700649
650std::atomic<int32_t> DisplayDeviceState::nextDisplayId(1);
651
652DisplayDeviceState::DisplayDeviceState(DisplayDevice::DisplayType type, bool isSecure)
653 : type(type),
654 layerStack(DisplayDevice::NO_LAYER_STACK),
655 orientation(0),
656 width(0),
657 height(0),
658 isSecure(isSecure)
659{
660 viewport.makeInvalid();
661 frame.makeInvalid();
662}