blob: 95802bd726b413c2d1e94f3d759d5661efbe8e0b [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
Mathias Agopianc666cae2012-07-25 18:56:13 -070031#include <ui/DisplayInfo.h>
Mathias Agopian076b1cc2009-04-10 14:24:30 -070032#include <ui/PixelFormat.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080033
Mathias Agopiane3c697f2013-02-14 17:11:02 -080034#include <gui/Surface.h>
Jamie Gennis1a4d8832012-08-02 20:11:05 -070035
Mathias Agopian076b1cc2009-04-10 14:24:30 -070036#include <hardware/gralloc.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080037
Jesse Hall99c7dbb2013-03-14 14:29:29 -070038#include "DisplayHardware/DisplaySurface.h"
Mathias Agopian1b031492012-06-20 17:51:20 -070039#include "DisplayHardware/HWComposer.h"
Fabien Sanglard9d96de42016-10-11 00:15:18 +000040#ifdef USE_HWC2
Dan Stoza9e56aa02015-11-02 13:00:03 -080041#include "DisplayHardware/HWC2.h"
Fabien Sanglard9d96de42016-10-11 00:15:18 +000042#endif
Mathias Agopian875d8e12013-06-07 15:35:48 -070043#include "RenderEngine/RenderEngine.h"
Mathias Agopian1b031492012-06-20 17:51:20 -070044
Mathias Agopianda8d0a52012-09-04 15:05:38 -070045#include "clz.h"
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -070046#include "DisplayDevice.h"
Mathias Agopianc7d14e22011-08-01 16:32:21 -070047#include "SurfaceFlinger.h"
Mathias Agopian13127d82013-03-05 17:47:11 -080048#include "Layer.h"
Mathias Agopian1f7bec62010-06-25 18:02:21 -070049
Jaesoo Lee720a7242017-01-31 15:26:18 +090050#include <android/hardware/configstore/1.0/ISurfaceFlingerConfigs.h>
51#include <configstore/Utils.h>
52
Mathias Agopiana4912602012-07-12 14:25:33 -070053// ----------------------------------------------------------------------------
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080054using namespace android;
Mathias Agopiana4912602012-07-12 14:25:33 -070055// ----------------------------------------------------------------------------
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080056
Andreas Gampe89fd4f72014-11-13 14:18:56 -080057#ifdef EGL_ANDROID_swap_rectangle
58static constexpr bool kEGLAndroidSwapRectangle = true;
59#else
60static constexpr bool kEGLAndroidSwapRectangle = false;
61#endif
62
Jaesoo Lee720a7242017-01-31 15:26:18 +090063// retrieve triple buffer setting from configstore
64using namespace android::hardware::configstore;
65using namespace android::hardware::configstore::V1_0;
66
Fabien Sanglard1971b632017-03-10 14:50:03 -080067static bool useTripleFramebuffer = getInt64< ISurfaceFlingerConfigs,
Fabien Sanglard48eb5652017-07-26 14:55:11 -070068 &ISurfaceFlingerConfigs::maxFrameBufferAcquiredBuffers>(2) >= 3;
Jaesoo Lee720a7242017-01-31 15:26:18 +090069
Andreas Gampe89fd4f72014-11-13 14:18:56 -080070#if !defined(EGL_EGLEXT_PROTOTYPES) || !defined(EGL_ANDROID_swap_rectangle)
71// Dummy implementation in case it is missing.
72inline void eglSetSwapRectangleANDROID (EGLDisplay, EGLSurface, EGLint, EGLint, EGLint, EGLint) {
73}
74#endif
75
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080076/*
77 * Initialize the display to the specified values.
78 *
79 */
80
Pablo Ceballos021623b2016-04-15 17:31:51 -070081uint32_t DisplayDevice::sPrimaryDisplayOrientation = 0;
82
Courtney Goeltzenleuchter5d943892017-03-22 13:46:46 -060083// clang-format off
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -070084DisplayDevice::DisplayDevice(
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080085 const sp<SurfaceFlinger>& flinger,
Jamie Gennisdd3cb842012-10-19 18:19:11 -070086 DisplayType type,
Jesse Hallffe1f192013-03-22 15:13:48 -070087 int32_t hwcId,
Fabien Sanglard9d96de42016-10-11 00:15:18 +000088#ifndef USE_HWC2
89 int format,
90#endif
Jamie Gennisdd3cb842012-10-19 18:19:11 -070091 bool isSecure,
92 const wp<IBinder>& displayToken,
Jesse Hall99c7dbb2013-03-14 14:29:29 -070093 const sp<DisplaySurface>& displaySurface,
Mathias Agopiandb89edc2013-08-02 01:40:18 -070094 const sp<IGraphicBufferProducer>& producer,
Courtney Goeltzenleuchter5d943892017-03-22 13:46:46 -060095 EGLConfig config,
96 bool supportWideColor)
Jesse Hallb7a05492014-08-14 15:45:06 -070097 : lastCompositionHadVisibleLayers(false),
98 mFlinger(flinger),
Dan Stoza9e56aa02015-11-02 13:00:03 -080099 mType(type),
100 mHwcDisplayId(hwcId),
Chih-Wei Huang27e25622013-01-07 17:33:56 +0800101 mDisplayToken(displayToken),
Jesse Hall99c7dbb2013-03-14 14:29:29 -0700102 mDisplaySurface(displaySurface),
Mathias Agopian92a979a2012-08-02 18:32:23 -0700103 mDisplay(EGL_NO_DISPLAY),
104 mSurface(EGL_NO_SURFACE),
Dan Stoza9e56aa02015-11-02 13:00:03 -0800105 mDisplayWidth(),
106 mDisplayHeight(),
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000107#ifndef USE_HWC2
108 mFormat(),
109#endif
Mathias Agopian92a979a2012-08-02 18:32:23 -0700110 mFlags(),
111 mPageFlipCount(),
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700112 mIsSecure(isSecure),
Jesse Hall01e29052013-02-19 16:13:35 -0800113 mLayerStack(NO_LAYER_STACK),
Prashant Malani2c9b11f2014-05-25 01:36:31 -0700114 mOrientation(),
Michael Lentine6c9e34a2014-07-14 13:48:55 -0700115 mPowerMode(HWC_POWER_MODE_OFF),
Courtney Goeltzenleuchter4f20f9c2017-04-06 08:18:34 -0600116 mActiveConfig(0)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800117{
Courtney Goeltzenleuchter5d943892017-03-22 13:46:46 -0600118 // clang-format on
james.zhang5e8eb5e2015-12-01 17:55:11 +0800119 Surface* surface;
120 mNativeWindow = surface = new Surface(producer, false);
Jesse Hallffe1f192013-03-22 15:13:48 -0700121 ANativeWindow* const window = mNativeWindow.get();
122
Courtney Goeltzenleuchter62caf7c2017-03-14 14:18:28 -0600123#ifdef USE_HWC2
Naseer Ahmed901a86e2017-05-11 11:15:21 -0400124 mActiveColorMode = HAL_COLOR_MODE_NATIVE;
Courtney Goeltzenleuchter4f20f9c2017-04-06 08:18:34 -0600125 mDisplayHasWideColor = supportWideColor;
126#else
127 (void) supportWideColor;
Courtney Goeltzenleuchter62caf7c2017-03-14 14:18:28 -0600128#endif
Jesse Hallffe1f192013-03-22 15:13:48 -0700129 /*
130 * Create our display's surface
131 */
132
james.zhang5e8eb5e2015-12-01 17:55:11 +0800133 EGLSurface eglSurface;
Jesse Hallffe1f192013-03-22 15:13:48 -0700134 EGLDisplay display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
Jesse Hall19e87292013-12-23 21:02:15 -0800135 if (config == EGL_NO_CONFIG) {
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000136#ifdef USE_HWC2
Steven Thomasb02664d2017-07-26 18:48:28 -0700137 config = RenderEngine::chooseEglConfig(display, PIXEL_FORMAT_RGBA_8888,
138 /*logConfig*/ false);
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000139#else
Steven Thomasb02664d2017-07-26 18:48:28 -0700140 config = RenderEngine::chooseEglConfig(display, format,
141 /*logConfig*/ false);
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000142#endif
Jesse Hall19e87292013-12-23 21:02:15 -0800143 }
james.zhang5e8eb5e2015-12-01 17:55:11 +0800144 eglSurface = eglCreateWindowSurface(display, config, window, NULL);
145 eglQuerySurface(display, eglSurface, EGL_WIDTH, &mDisplayWidth);
146 eglQuerySurface(display, eglSurface, EGL_HEIGHT, &mDisplayHeight);
Jesse Hallffe1f192013-03-22 15:13:48 -0700147
John Dong4ee56962014-02-21 12:37:59 -0800148 // Make sure that composition can never be stalled by a virtual display
149 // consumer that isn't processing buffers fast enough. We have to do this
150 // in two places:
151 // * Here, in case the display is composed entirely by HWC.
152 // * In makeCurrent(), using eglSwapInterval. Some EGL drivers set the
153 // window's swap interval in eglMakeCurrent, so they'll override the
154 // interval we set here.
155 if (mType >= DisplayDevice::DISPLAY_VIRTUAL)
156 window->setSwapInterval(window, 0);
157
Michael Lentine47e45402014-07-18 15:34:25 -0700158 mConfig = config;
Jesse Hallffe1f192013-03-22 15:13:48 -0700159 mDisplay = display;
james.zhang5e8eb5e2015-12-01 17:55:11 +0800160 mSurface = eglSurface;
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000161#ifndef USE_HWC2
162 mFormat = format;
163#endif
Jesse Hallffe1f192013-03-22 15:13:48 -0700164 mPageFlipCount = 0;
165 mViewport.makeInvalid();
166 mFrame.makeInvalid();
167
168 // virtual displays are always considered enabled
Prashant Malani2c9b11f2014-05-25 01:36:31 -0700169 mPowerMode = (mType >= DisplayDevice::DISPLAY_VIRTUAL) ?
170 HWC_POWER_MODE_NORMAL : HWC_POWER_MODE_OFF;
Jesse Hallffe1f192013-03-22 15:13:48 -0700171
172 // Name the display. The name will be replaced shortly if the display
173 // was created with createDisplay().
174 switch (mType) {
175 case DISPLAY_PRIMARY:
176 mDisplayName = "Built-in Screen";
177 break;
178 case DISPLAY_EXTERNAL:
179 mDisplayName = "HDMI Screen";
180 break;
181 default:
182 mDisplayName = "Virtual Screen"; // e.g. Overlay #n
183 break;
184 }
185
186 // initialize the display orientation transform.
187 setProjection(DisplayState::eOrientationDefault, mViewport, mFrame);
james.zhang5e8eb5e2015-12-01 17:55:11 +0800188
Jaesoo Lee720a7242017-01-31 15:26:18 +0900189 if (useTripleFramebuffer) {
190 surface->allocateBuffers();
191 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800192}
193
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700194DisplayDevice::~DisplayDevice() {
Mathias Agopian92a979a2012-08-02 18:32:23 -0700195 if (mSurface != EGL_NO_SURFACE) {
196 eglDestroySurface(mDisplay, mSurface);
197 mSurface = EGL_NO_SURFACE;
198 }
199}
200
Jesse Hall02d86562013-03-25 14:43:23 -0700201void DisplayDevice::disconnect(HWComposer& hwc) {
202 if (mHwcDisplayId >= 0) {
203 hwc.disconnectDisplay(mHwcDisplayId);
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000204#ifndef USE_HWC2
205 if (mHwcDisplayId >= DISPLAY_VIRTUAL)
206 hwc.freeDisplayId(mHwcDisplayId);
207#endif
Jesse Hall02d86562013-03-25 14:43:23 -0700208 mHwcDisplayId = -1;
209 }
210}
211
Mathias Agopian92a979a2012-08-02 18:32:23 -0700212bool DisplayDevice::isValid() const {
213 return mFlinger != NULL;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800214}
215
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700216int DisplayDevice::getWidth() const {
Mathias Agopiana4912602012-07-12 14:25:33 -0700217 return mDisplayWidth;
218}
219
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700220int DisplayDevice::getHeight() const {
Mathias Agopiana4912602012-07-12 14:25:33 -0700221 return mDisplayHeight;
222}
223
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000224#ifndef USE_HWC2
225PixelFormat DisplayDevice::getFormat() const {
226 return mFormat;
227}
228#endif
229
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700230EGLSurface DisplayDevice::getEGLSurface() const {
Mathias Agopiana4912602012-07-12 14:25:33 -0700231 return mSurface;
232}
233
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700234void DisplayDevice::setDisplayName(const String8& displayName) {
235 if (!displayName.isEmpty()) {
236 // never override the name with an empty name
237 mDisplayName = displayName;
238 }
239}
240
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700241uint32_t DisplayDevice::getPageFlipCount() const {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700242 return mPageFlipCount;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800243}
244
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000245#ifndef USE_HWC2
246status_t DisplayDevice::compositionComplete() const {
247 return mDisplaySurface->compositionComplete();
248}
249#endif
250
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700251void DisplayDevice::flip(const Region& dirty) const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800252{
Mathias Agopian875d8e12013-06-07 15:35:48 -0700253 mFlinger->getRenderEngine().checkErrors();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800254
Andreas Gampe89fd4f72014-11-13 14:18:56 -0800255 if (kEGLAndroidSwapRectangle) {
256 if (mFlags & SWAP_RECTANGLE) {
257 const Region newDirty(dirty.intersect(bounds()));
258 const Rect b(newDirty.getBounds());
259 eglSetSwapRectangleANDROID(mDisplay, mSurface,
260 b.left, b.top, b.width(), b.height());
261 }
Jesse Hall01e29052013-02-19 16:13:35 -0800262 }
Mathias Agopiand8707032012-09-18 01:21:55 -0700263
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700264 mPageFlipCount++;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800265}
266
Dan Stoza71433162014-02-04 16:22:36 -0800267status_t DisplayDevice::beginFrame(bool mustRecompose) const {
268 return mDisplaySurface->beginFrame(mustRecompose);
Jesse Hall028dc8f2013-08-20 16:35:32 -0700269}
270
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000271#ifdef USE_HWC2
Dan Stoza9e56aa02015-11-02 13:00:03 -0800272status_t DisplayDevice::prepareFrame(HWComposer& hwc) {
273 status_t error = hwc.prepare(*this);
274 if (error != NO_ERROR) {
275 return error;
276 }
277
278 DisplaySurface::CompositionType compositionType;
279 bool hasClient = hwc.hasClientComposition(mHwcDisplayId);
280 bool hasDevice = hwc.hasDeviceComposition(mHwcDisplayId);
281 if (hasClient && hasDevice) {
282 compositionType = DisplaySurface::COMPOSITION_MIXED;
283 } else if (hasClient) {
284 compositionType = DisplaySurface::COMPOSITION_GLES;
285 } else if (hasDevice) {
286 compositionType = DisplaySurface::COMPOSITION_HWC;
287 } else {
288 // Nothing to do -- when turning the screen off we get a frame like
289 // this. Call it a HWC frame since we won't be doing any GLES work but
290 // will do a prepare/set cycle.
291 compositionType = DisplaySurface::COMPOSITION_HWC;
292 }
293 return mDisplaySurface->prepareFrame(compositionType);
294}
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000295#else
296status_t DisplayDevice::prepareFrame(const HWComposer& hwc) const {
297 DisplaySurface::CompositionType compositionType;
298 bool haveGles = hwc.hasGlesComposition(mHwcDisplayId);
299 bool haveHwc = hwc.hasHwcComposition(mHwcDisplayId);
300 if (haveGles && haveHwc) {
301 compositionType = DisplaySurface::COMPOSITION_MIXED;
302 } else if (haveGles) {
303 compositionType = DisplaySurface::COMPOSITION_GLES;
304 } else if (haveHwc) {
305 compositionType = DisplaySurface::COMPOSITION_HWC;
306 } else {
307 // Nothing to do -- when turning the screen off we get a frame like
308 // this. Call it a HWC frame since we won't be doing any GLES work but
309 // will do a prepare/set cycle.
310 compositionType = DisplaySurface::COMPOSITION_HWC;
311 }
312 return mDisplaySurface->prepareFrame(compositionType);
313}
314#endif
Jesse Hall38efe862013-04-06 23:12:29 -0700315
Mathias Agopianda27af92012-09-13 18:17:13 -0700316void DisplayDevice::swapBuffers(HWComposer& hwc) const {
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000317#ifdef USE_HWC2
Dan Stoza9e56aa02015-11-02 13:00:03 -0800318 if (hwc.hasClientComposition(mHwcDisplayId)) {
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000319#else
320 // We need to call eglSwapBuffers() if:
321 // (1) we don't have a hardware composer, or
322 // (2) we did GLES composition this frame, and either
323 // (a) we have framebuffer target support (not present on legacy
324 // devices, where HWComposer::commit() handles things); or
325 // (b) this is a virtual display
326 if (hwc.initCheck() != NO_ERROR ||
327 (hwc.hasGlesComposition(mHwcDisplayId) &&
328 (hwc.supportsFramebufferTarget() || mType >= DISPLAY_VIRTUAL))) {
329#endif
Jesse Hall99c7dbb2013-03-14 14:29:29 -0700330 EGLBoolean success = eglSwapBuffers(mDisplay, mSurface);
331 if (!success) {
332 EGLint error = eglGetError();
333 if (error == EGL_CONTEXT_LOST ||
334 mType == DisplayDevice::DISPLAY_PRIMARY) {
335 LOG_ALWAYS_FATAL("eglSwapBuffers(%p, %p) failed with 0x%08x",
336 mDisplay, mSurface, error);
337 } else {
338 ALOGE("eglSwapBuffers(%p, %p) failed with 0x%08x",
339 mDisplay, mSurface, error);
Mathias Agopianda27af92012-09-13 18:17:13 -0700340 }
341 }
342 }
Mathias Agopian52e21482012-09-24 18:07:21 -0700343
Jesse Hall99c7dbb2013-03-14 14:29:29 -0700344 status_t result = mDisplaySurface->advanceFrame();
345 if (result != NO_ERROR) {
346 ALOGE("[%s] failed pushing new frame to HWC: %d",
347 mDisplayName.string(), result);
Mathias Agopian32341382012-09-25 19:16:28 -0700348 }
Mathias Agopianda27af92012-09-13 18:17:13 -0700349}
350
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000351#ifdef USE_HWC2
Dan Stoza9e56aa02015-11-02 13:00:03 -0800352void DisplayDevice::onSwapBuffersCompleted() const {
353 mDisplaySurface->onFrameCommitted();
354}
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000355#else
356void DisplayDevice::onSwapBuffersCompleted(HWComposer& hwc) const {
357 if (hwc.initCheck() == NO_ERROR) {
358 mDisplaySurface->onFrameCommitted();
359 }
360}
361#endif
Mathias Agopianda27af92012-09-13 18:17:13 -0700362
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700363uint32_t DisplayDevice::getFlags() const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800364{
365 return mFlags;
366}
367
Mathias Agopian875d8e12013-06-07 15:35:48 -0700368EGLBoolean DisplayDevice::makeCurrent(EGLDisplay dpy, EGLContext ctx) const {
Mathias Agopianda8d0a52012-09-04 15:05:38 -0700369 EGLBoolean result = EGL_TRUE;
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700370 EGLSurface sur = eglGetCurrentSurface(EGL_DRAW);
Mathias Agopian875d8e12013-06-07 15:35:48 -0700371 if (sur != mSurface) {
372 result = eglMakeCurrent(dpy, mSurface, mSurface, ctx);
Mathias Agopianda8d0a52012-09-04 15:05:38 -0700373 if (result == EGL_TRUE) {
Jesse Hallf460f552013-08-06 17:08:53 -0700374 if (mType >= DisplayDevice::DISPLAY_VIRTUAL)
375 eglSwapInterval(dpy, 0);
Mathias Agopianda8d0a52012-09-04 15:05:38 -0700376 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700377 }
Mathias Agopian931bda12013-08-28 18:11:46 -0700378 setViewportAndProjection();
Mathias Agopianda8d0a52012-09-04 15:05:38 -0700379 return result;
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700380}
381
Mathias Agopian875d8e12013-06-07 15:35:48 -0700382void DisplayDevice::setViewportAndProjection() const {
383 size_t w = mDisplayWidth;
384 size_t h = mDisplayHeight;
Dan Stozac1879002014-05-22 15:59:05 -0700385 Rect sourceCrop(0, 0, w, h);
Riley Andrewsc3ebe662014-09-04 16:20:31 -0700386 mFlinger->getRenderEngine().setViewportAndProjection(w, h, sourceCrop, h,
387 false, Transform::ROT_0);
Mathias Agopianbae92d02012-09-28 01:00:47 -0700388}
389
Dan Stoza9e56aa02015-11-02 13:00:03 -0800390const sp<Fence>& DisplayDevice::getClientTargetAcquireFence() const {
391 return mDisplaySurface->getClientTargetAcquireFence();
392}
Dan Stoza9e56aa02015-11-02 13:00:03 -0800393
Mathias Agopian1b031492012-06-20 17:51:20 -0700394// ----------------------------------------------------------------------------
395
Mathias Agopian13127d82013-03-05 17:47:11 -0800396void DisplayDevice::setVisibleLayersSortedByZ(const Vector< sp<Layer> >& layers) {
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700397 mVisibleLayersSortedByZ = layers;
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700398}
399
Mathias Agopian13127d82013-03-05 17:47:11 -0800400const Vector< sp<Layer> >& DisplayDevice::getVisibleLayersSortedByZ() const {
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700401 return mVisibleLayersSortedByZ;
402}
403
Chia-I Wuf9fc8ee2017-11-16 11:45:52 -0800404void DisplayDevice::setLayersNeedingFences(const Vector< sp<Layer> >& layers) {
405 mLayersNeedingFences = layers;
406}
407
408const Vector< sp<Layer> >& DisplayDevice::getLayersNeedingFences() const {
409 return mLayersNeedingFences;
410}
411
Mathias Agopiancd60f992012-08-16 16:28:27 -0700412Region DisplayDevice::getDirtyRegion(bool repaintEverything) const {
413 Region dirty;
Mathias Agopiancd60f992012-08-16 16:28:27 -0700414 if (repaintEverything) {
415 dirty.set(getBounds());
416 } else {
Mathias Agopianda8d0a52012-09-04 15:05:38 -0700417 const Transform& planeTransform(mGlobalTransform);
Mathias Agopiancd60f992012-08-16 16:28:27 -0700418 dirty = planeTransform.transform(this->dirtyRegion);
419 dirty.andSelf(getBounds());
420 }
421 return dirty;
422}
423
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700424// ----------------------------------------------------------------------------
Prashant Malani2c9b11f2014-05-25 01:36:31 -0700425void DisplayDevice::setPowerMode(int mode) {
426 mPowerMode = mode;
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700427}
428
Prashant Malani2c9b11f2014-05-25 01:36:31 -0700429int DisplayDevice::getPowerMode() const {
430 return mPowerMode;
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700431}
432
Prashant Malani2c9b11f2014-05-25 01:36:31 -0700433bool DisplayDevice::isDisplayOn() const {
434 return (mPowerMode != HWC_POWER_MODE_OFF);
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700435}
436
437// ----------------------------------------------------------------------------
Michael Lentine6c9e34a2014-07-14 13:48:55 -0700438void DisplayDevice::setActiveConfig(int mode) {
439 mActiveConfig = mode;
440}
441
442int DisplayDevice::getActiveConfig() const {
443 return mActiveConfig;
444}
445
446// ----------------------------------------------------------------------------
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000447#ifdef USE_HWC2
Michael Wright28f24d02016-07-12 13:30:53 -0700448void DisplayDevice::setActiveColorMode(android_color_mode_t mode) {
449 mActiveColorMode = mode;
450}
451
452android_color_mode_t DisplayDevice::getActiveColorMode() const {
453 return mActiveColorMode;
454}
Courtney Goeltzenleuchter79d27242017-07-13 17:54:01 -0600455
456void DisplayDevice::setCompositionDataSpace(android_dataspace dataspace) {
457 ANativeWindow* const window = mNativeWindow.get();
458 native_window_set_buffers_data_space(window, dataspace);
459}
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000460#endif
Michael Wright28f24d02016-07-12 13:30:53 -0700461
462// ----------------------------------------------------------------------------
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700463
Mathias Agopian28947d72012-08-08 18:51:15 -0700464void DisplayDevice::setLayerStack(uint32_t stack) {
465 mLayerStack = stack;
466 dirtyRegion.set(bounds());
467}
468
469// ----------------------------------------------------------------------------
470
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700471uint32_t DisplayDevice::getOrientationTransform() const {
472 uint32_t transform = 0;
473 switch (mOrientation) {
474 case DisplayState::eOrientationDefault:
475 transform = Transform::ROT_0;
476 break;
477 case DisplayState::eOrientation90:
478 transform = Transform::ROT_90;
479 break;
480 case DisplayState::eOrientation180:
481 transform = Transform::ROT_180;
482 break;
483 case DisplayState::eOrientation270:
484 transform = Transform::ROT_270;
485 break;
486 }
487 return transform;
488}
489
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700490status_t DisplayDevice::orientationToTransfrom(
Mathias Agopian1b031492012-06-20 17:51:20 -0700491 int orientation, int w, int h, Transform* tr)
492{
493 uint32_t flags = 0;
494 switch (orientation) {
Mathias Agopian3165cc22012-08-08 19:42:09 -0700495 case DisplayState::eOrientationDefault:
Mathias Agopian1b031492012-06-20 17:51:20 -0700496 flags = Transform::ROT_0;
497 break;
Mathias Agopian3165cc22012-08-08 19:42:09 -0700498 case DisplayState::eOrientation90:
Mathias Agopian1b031492012-06-20 17:51:20 -0700499 flags = Transform::ROT_90;
500 break;
Mathias Agopian3165cc22012-08-08 19:42:09 -0700501 case DisplayState::eOrientation180:
Mathias Agopian1b031492012-06-20 17:51:20 -0700502 flags = Transform::ROT_180;
503 break;
Mathias Agopian3165cc22012-08-08 19:42:09 -0700504 case DisplayState::eOrientation270:
Mathias Agopian1b031492012-06-20 17:51:20 -0700505 flags = Transform::ROT_270;
506 break;
507 default:
508 return BAD_VALUE;
509 }
510 tr->set(flags, w, h);
511 return NO_ERROR;
512}
513
Michael Lentine47e45402014-07-18 15:34:25 -0700514void DisplayDevice::setDisplaySize(const int newWidth, const int newHeight) {
515 dirtyRegion.set(getBounds());
516
Michael Lentinef2568de2014-08-20 10:51:23 -0700517 if (mSurface != EGL_NO_SURFACE) {
518 eglDestroySurface(mDisplay, mSurface);
519 mSurface = EGL_NO_SURFACE;
520 }
521
Michael Lentine47e45402014-07-18 15:34:25 -0700522 mDisplaySurface->resizeBuffers(newWidth, newHeight);
523
524 ANativeWindow* const window = mNativeWindow.get();
525 mSurface = eglCreateWindowSurface(mDisplay, mConfig, window, NULL);
526 eglQuerySurface(mDisplay, mSurface, EGL_WIDTH, &mDisplayWidth);
527 eglQuerySurface(mDisplay, mSurface, EGL_HEIGHT, &mDisplayHeight);
528
529 LOG_FATAL_IF(mDisplayWidth != newWidth,
530 "Unable to set new width to %d", newWidth);
531 LOG_FATAL_IF(mDisplayHeight != newHeight,
532 "Unable to set new height to %d", newHeight);
533}
534
Mathias Agopian00e8c7a2012-09-04 19:30:46 -0700535void DisplayDevice::setProjection(int orientation,
Mathias Agopianf5f714a2013-02-26 16:54:05 -0800536 const Rect& newViewport, const Rect& newFrame) {
537 Rect viewport(newViewport);
538 Rect frame(newFrame);
539
540 const int w = mDisplayWidth;
541 const int h = mDisplayHeight;
542
543 Transform R;
544 DisplayDevice::orientationToTransfrom(orientation, w, h, &R);
545
546 if (!frame.isValid()) {
547 // the destination frame can be invalid if it has never been set,
548 // in that case we assume the whole display frame.
549 frame = Rect(w, h);
550 }
551
552 if (viewport.isEmpty()) {
553 // viewport can be invalid if it has never been set, in that case
554 // we assume the whole display size.
555 // it's also invalid to have an empty viewport, so we handle that
556 // case in the same way.
557 viewport = Rect(w, h);
558 if (R.getOrientation() & Transform::ROT_90) {
559 // viewport is always specified in the logical orientation
560 // of the display (ie: post-rotation).
561 swap(viewport.right, viewport.bottom);
562 }
563 }
564
565 dirtyRegion.set(getBounds());
566
567 Transform TL, TP, S;
568 float src_width = viewport.width();
569 float src_height = viewport.height();
570 float dst_width = frame.width();
571 float dst_height = frame.height();
572 if (src_width != dst_width || src_height != dst_height) {
573 float sx = dst_width / src_width;
574 float sy = dst_height / src_height;
575 S.set(sx, 0, 0, sy);
576 }
577
578 float src_x = viewport.left;
579 float src_y = viewport.top;
580 float dst_x = frame.left;
581 float dst_y = frame.top;
582 TL.set(-src_x, -src_y);
583 TP.set(dst_x, dst_y);
584
585 // The viewport and frame are both in the logical orientation.
586 // Apply the logical translation, scale to physical size, apply the
587 // physical translation and finally rotate to the physical orientation.
588 mGlobalTransform = R * TP * S * TL;
589
590 const uint8_t type = mGlobalTransform.getType();
591 mNeedsFiltering = (!mGlobalTransform.preserveRects() ||
592 (type >= Transform::SCALE));
593
594 mScissor = mGlobalTransform.transform(viewport);
595 if (mScissor.isEmpty()) {
Mathias Agopian6c7f25a2013-05-09 20:37:10 -0700596 mScissor = getBounds();
Mathias Agopianf5f714a2013-02-26 16:54:05 -0800597 }
598
Mathias Agopianda8d0a52012-09-04 15:05:38 -0700599 mOrientation = orientation;
Pablo Ceballos021623b2016-04-15 17:31:51 -0700600 if (mType == DisplayType::DISPLAY_PRIMARY) {
601 uint32_t transform = 0;
602 switch (mOrientation) {
603 case DisplayState::eOrientationDefault:
604 transform = Transform::ROT_0;
605 break;
606 case DisplayState::eOrientation90:
607 transform = Transform::ROT_90;
608 break;
609 case DisplayState::eOrientation180:
610 transform = Transform::ROT_180;
611 break;
612 case DisplayState::eOrientation270:
613 transform = Transform::ROT_270;
614 break;
615 }
616 sPrimaryDisplayOrientation = transform;
617 }
Mathias Agopian00e8c7a2012-09-04 19:30:46 -0700618 mViewport = viewport;
619 mFrame = frame;
Mathias Agopian1b031492012-06-20 17:51:20 -0700620}
Mathias Agopian1d12d8a2012-09-18 01:38:00 -0700621
Pablo Ceballos021623b2016-04-15 17:31:51 -0700622uint32_t DisplayDevice::getPrimaryDisplayOrientationTransform() {
623 return sPrimaryDisplayOrientation;
624}
625
Mathias Agopian74d211a2013-04-22 16:55:35 +0200626void DisplayDevice::dump(String8& result) const {
Mathias Agopian1d12d8a2012-09-18 01:38:00 -0700627 const Transform& tr(mGlobalTransform);
Courtney Goeltzenleuchter0ebaac32017-04-13 12:17:03 -0600628 EGLint redSize, greenSize, blueSize, alphaSize;
629 eglGetConfigAttrib(mDisplay, mConfig, EGL_RED_SIZE, &redSize);
630 eglGetConfigAttrib(mDisplay, mConfig, EGL_GREEN_SIZE, &greenSize);
631 eglGetConfigAttrib(mDisplay, mConfig, EGL_BLUE_SIZE, &blueSize);
632 eglGetConfigAttrib(mDisplay, mConfig, EGL_ALPHA_SIZE, &alphaSize);
633 result.appendFormat("+ DisplayDevice: %s\n", mDisplayName.string());
634 result.appendFormat(" type=%x, hwcId=%d, layerStack=%u, (%4dx%4d), ANativeWindow=%p "
635 "(%d:%d:%d:%d), orient=%2d (type=%08x), "
636 "flips=%u, isSecure=%d, powerMode=%d, activeConfig=%d, numLayers=%zu\n",
637 mType, mHwcDisplayId, mLayerStack, mDisplayWidth, mDisplayHeight,
638 mNativeWindow.get(), redSize, greenSize, blueSize, alphaSize, mOrientation,
639 tr.getType(), getPageFlipCount(), mIsSecure, mPowerMode, mActiveConfig,
640 mVisibleLayersSortedByZ.size());
641 result.appendFormat(" v:[%d,%d,%d,%d], f:[%d,%d,%d,%d], s:[%d,%d,%d,%d],"
642 "transform:[[%0.3f,%0.3f,%0.3f][%0.3f,%0.3f,%0.3f][%0.3f,%0.3f,%0.3f]]\n",
643 mViewport.left, mViewport.top, mViewport.right, mViewport.bottom,
644 mFrame.left, mFrame.top, mFrame.right, mFrame.bottom, mScissor.left,
645 mScissor.top, mScissor.right, mScissor.bottom, tr[0][0], tr[1][0], tr[2][0],
646 tr[0][1], tr[1][1], tr[2][1], tr[0][2], tr[1][2], tr[2][2]);
Mathias Agopian1d12d8a2012-09-18 01:38:00 -0700647
Jesse Hall99c7dbb2013-03-14 14:29:29 -0700648 String8 surfaceDump;
Dan Stozaf10c46e2014-11-11 10:32:31 -0800649 mDisplaySurface->dumpAsString(surfaceDump);
Jesse Hall99c7dbb2013-03-14 14:29:29 -0700650 result.append(surfaceDump);
Mathias Agopian1d12d8a2012-09-18 01:38:00 -0700651}
Irvelffc9efc2016-07-27 15:16:37 -0700652
653std::atomic<int32_t> DisplayDeviceState::nextDisplayId(1);
654
655DisplayDeviceState::DisplayDeviceState(DisplayDevice::DisplayType type, bool isSecure)
656 : type(type),
657 layerStack(DisplayDevice::NO_LAYER_STACK),
658 orientation(0),
659 width(0),
660 height(0),
661 isSecure(isSecure)
662{
663 viewport.makeInvalid();
664 frame.makeInvalid();
665}