blob: 11df4e24c9b8a871a1f1e71a3ed65bd96f1fbb2d [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
67static bool useTripleFramebuffer = getBool<
68 ISurfaceFlingerConfigs,
69 &ISurfaceFlingerConfigs::useTripleFramebuffer>(false);
70
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
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,
Mathias Agopiana4912602012-07-12 14:25:33 -070095 EGLConfig config)
Jesse Hallb7a05492014-08-14 15:45:06 -070096 : lastCompositionHadVisibleLayers(false),
97 mFlinger(flinger),
Dan Stoza9e56aa02015-11-02 13:00:03 -080098 mType(type),
99 mHwcDisplayId(hwcId),
Chih-Wei Huang27e25622013-01-07 17:33:56 +0800100 mDisplayToken(displayToken),
Jesse Hall99c7dbb2013-03-14 14:29:29 -0700101 mDisplaySurface(displaySurface),
Mathias Agopian92a979a2012-08-02 18:32:23 -0700102 mDisplay(EGL_NO_DISPLAY),
103 mSurface(EGL_NO_SURFACE),
Dan Stoza9e56aa02015-11-02 13:00:03 -0800104 mDisplayWidth(),
105 mDisplayHeight(),
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000106#ifndef USE_HWC2
107 mFormat(),
108#endif
Mathias Agopian92a979a2012-08-02 18:32:23 -0700109 mFlags(),
110 mPageFlipCount(),
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700111 mIsSecure(isSecure),
Jesse Hall01e29052013-02-19 16:13:35 -0800112 mLayerStack(NO_LAYER_STACK),
Prashant Malani2c9b11f2014-05-25 01:36:31 -0700113 mOrientation(),
Michael Lentine6c9e34a2014-07-14 13:48:55 -0700114 mPowerMode(HWC_POWER_MODE_OFF),
115 mActiveConfig(0)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800116{
james.zhang5e8eb5e2015-12-01 17:55:11 +0800117 Surface* surface;
118 mNativeWindow = surface = new Surface(producer, false);
Jesse Hallffe1f192013-03-22 15:13:48 -0700119 ANativeWindow* const window = mNativeWindow.get();
120
Courtney Goeltzenleuchter62caf7c2017-03-14 14:18:28 -0600121#ifdef USE_HWC2
122 mActiveColorMode = static_cast<android_color_mode_t>(-1);
123#endif
Jesse Hallffe1f192013-03-22 15:13:48 -0700124 /*
125 * Create our display's surface
126 */
127
james.zhang5e8eb5e2015-12-01 17:55:11 +0800128 EGLSurface eglSurface;
Jesse Hallffe1f192013-03-22 15:13:48 -0700129 EGLDisplay display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
Jesse Hall19e87292013-12-23 21:02:15 -0800130 if (config == EGL_NO_CONFIG) {
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000131#ifdef USE_HWC2
Dan Stoza9e56aa02015-11-02 13:00:03 -0800132 config = RenderEngine::chooseEglConfig(display, PIXEL_FORMAT_RGBA_8888);
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000133#else
134 config = RenderEngine::chooseEglConfig(display, format);
135#endif
Jesse Hall19e87292013-12-23 21:02:15 -0800136 }
james.zhang5e8eb5e2015-12-01 17:55:11 +0800137 eglSurface = eglCreateWindowSurface(display, config, window, NULL);
138 eglQuerySurface(display, eglSurface, EGL_WIDTH, &mDisplayWidth);
139 eglQuerySurface(display, eglSurface, EGL_HEIGHT, &mDisplayHeight);
Jesse Hallffe1f192013-03-22 15:13:48 -0700140
John Dong4ee56962014-02-21 12:37:59 -0800141 // Make sure that composition can never be stalled by a virtual display
142 // consumer that isn't processing buffers fast enough. We have to do this
143 // in two places:
144 // * Here, in case the display is composed entirely by HWC.
145 // * In makeCurrent(), using eglSwapInterval. Some EGL drivers set the
146 // window's swap interval in eglMakeCurrent, so they'll override the
147 // interval we set here.
148 if (mType >= DisplayDevice::DISPLAY_VIRTUAL)
149 window->setSwapInterval(window, 0);
150
Michael Lentine47e45402014-07-18 15:34:25 -0700151 mConfig = config;
Jesse Hallffe1f192013-03-22 15:13:48 -0700152 mDisplay = display;
james.zhang5e8eb5e2015-12-01 17:55:11 +0800153 mSurface = eglSurface;
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000154#ifndef USE_HWC2
155 mFormat = format;
156#endif
Jesse Hallffe1f192013-03-22 15:13:48 -0700157 mPageFlipCount = 0;
158 mViewport.makeInvalid();
159 mFrame.makeInvalid();
160
161 // virtual displays are always considered enabled
Prashant Malani2c9b11f2014-05-25 01:36:31 -0700162 mPowerMode = (mType >= DisplayDevice::DISPLAY_VIRTUAL) ?
163 HWC_POWER_MODE_NORMAL : HWC_POWER_MODE_OFF;
Jesse Hallffe1f192013-03-22 15:13:48 -0700164
165 // Name the display. The name will be replaced shortly if the display
166 // was created with createDisplay().
167 switch (mType) {
168 case DISPLAY_PRIMARY:
169 mDisplayName = "Built-in Screen";
170 break;
171 case DISPLAY_EXTERNAL:
172 mDisplayName = "HDMI Screen";
173 break;
174 default:
175 mDisplayName = "Virtual Screen"; // e.g. Overlay #n
176 break;
177 }
178
179 // initialize the display orientation transform.
180 setProjection(DisplayState::eOrientationDefault, mViewport, mFrame);
james.zhang5e8eb5e2015-12-01 17:55:11 +0800181
Jaesoo Lee720a7242017-01-31 15:26:18 +0900182 if (useTripleFramebuffer) {
183 surface->allocateBuffers();
184 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800185}
186
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700187DisplayDevice::~DisplayDevice() {
Mathias Agopian92a979a2012-08-02 18:32:23 -0700188 if (mSurface != EGL_NO_SURFACE) {
189 eglDestroySurface(mDisplay, mSurface);
190 mSurface = EGL_NO_SURFACE;
191 }
192}
193
Jesse Hall02d86562013-03-25 14:43:23 -0700194void DisplayDevice::disconnect(HWComposer& hwc) {
195 if (mHwcDisplayId >= 0) {
196 hwc.disconnectDisplay(mHwcDisplayId);
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000197#ifndef USE_HWC2
198 if (mHwcDisplayId >= DISPLAY_VIRTUAL)
199 hwc.freeDisplayId(mHwcDisplayId);
200#endif
Jesse Hall02d86562013-03-25 14:43:23 -0700201 mHwcDisplayId = -1;
202 }
203}
204
Mathias Agopian92a979a2012-08-02 18:32:23 -0700205bool DisplayDevice::isValid() const {
206 return mFlinger != NULL;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800207}
208
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700209int DisplayDevice::getWidth() const {
Mathias Agopiana4912602012-07-12 14:25:33 -0700210 return mDisplayWidth;
211}
212
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700213int DisplayDevice::getHeight() const {
Mathias Agopiana4912602012-07-12 14:25:33 -0700214 return mDisplayHeight;
215}
216
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000217#ifndef USE_HWC2
218PixelFormat DisplayDevice::getFormat() const {
219 return mFormat;
220}
221#endif
222
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700223EGLSurface DisplayDevice::getEGLSurface() const {
Mathias Agopiana4912602012-07-12 14:25:33 -0700224 return mSurface;
225}
226
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700227void DisplayDevice::setDisplayName(const String8& displayName) {
228 if (!displayName.isEmpty()) {
229 // never override the name with an empty name
230 mDisplayName = displayName;
231 }
232}
233
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700234uint32_t DisplayDevice::getPageFlipCount() const {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700235 return mPageFlipCount;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800236}
237
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000238#ifndef USE_HWC2
239status_t DisplayDevice::compositionComplete() const {
240 return mDisplaySurface->compositionComplete();
241}
242#endif
243
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700244void DisplayDevice::flip(const Region& dirty) const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800245{
Mathias Agopian875d8e12013-06-07 15:35:48 -0700246 mFlinger->getRenderEngine().checkErrors();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800247
Andreas Gampe89fd4f72014-11-13 14:18:56 -0800248 if (kEGLAndroidSwapRectangle) {
249 if (mFlags & SWAP_RECTANGLE) {
250 const Region newDirty(dirty.intersect(bounds()));
251 const Rect b(newDirty.getBounds());
252 eglSetSwapRectangleANDROID(mDisplay, mSurface,
253 b.left, b.top, b.width(), b.height());
254 }
Jesse Hall01e29052013-02-19 16:13:35 -0800255 }
Mathias Agopiand8707032012-09-18 01:21:55 -0700256
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700257 mPageFlipCount++;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800258}
259
Dan Stoza71433162014-02-04 16:22:36 -0800260status_t DisplayDevice::beginFrame(bool mustRecompose) const {
261 return mDisplaySurface->beginFrame(mustRecompose);
Jesse Hall028dc8f2013-08-20 16:35:32 -0700262}
263
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000264#ifdef USE_HWC2
Dan Stoza9e56aa02015-11-02 13:00:03 -0800265status_t DisplayDevice::prepareFrame(HWComposer& hwc) {
266 status_t error = hwc.prepare(*this);
267 if (error != NO_ERROR) {
268 return error;
269 }
270
271 DisplaySurface::CompositionType compositionType;
272 bool hasClient = hwc.hasClientComposition(mHwcDisplayId);
273 bool hasDevice = hwc.hasDeviceComposition(mHwcDisplayId);
274 if (hasClient && hasDevice) {
275 compositionType = DisplaySurface::COMPOSITION_MIXED;
276 } else if (hasClient) {
277 compositionType = DisplaySurface::COMPOSITION_GLES;
278 } else if (hasDevice) {
279 compositionType = DisplaySurface::COMPOSITION_HWC;
280 } else {
281 // Nothing to do -- when turning the screen off we get a frame like
282 // this. Call it a HWC frame since we won't be doing any GLES work but
283 // will do a prepare/set cycle.
284 compositionType = DisplaySurface::COMPOSITION_HWC;
285 }
286 return mDisplaySurface->prepareFrame(compositionType);
287}
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000288#else
289status_t DisplayDevice::prepareFrame(const HWComposer& hwc) const {
290 DisplaySurface::CompositionType compositionType;
291 bool haveGles = hwc.hasGlesComposition(mHwcDisplayId);
292 bool haveHwc = hwc.hasHwcComposition(mHwcDisplayId);
293 if (haveGles && haveHwc) {
294 compositionType = DisplaySurface::COMPOSITION_MIXED;
295 } else if (haveGles) {
296 compositionType = DisplaySurface::COMPOSITION_GLES;
297 } else if (haveHwc) {
298 compositionType = DisplaySurface::COMPOSITION_HWC;
299 } else {
300 // Nothing to do -- when turning the screen off we get a frame like
301 // this. Call it a HWC frame since we won't be doing any GLES work but
302 // will do a prepare/set cycle.
303 compositionType = DisplaySurface::COMPOSITION_HWC;
304 }
305 return mDisplaySurface->prepareFrame(compositionType);
306}
307#endif
Jesse Hall38efe862013-04-06 23:12:29 -0700308
Mathias Agopianda27af92012-09-13 18:17:13 -0700309void DisplayDevice::swapBuffers(HWComposer& hwc) const {
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000310#ifdef USE_HWC2
Dan Stoza9e56aa02015-11-02 13:00:03 -0800311 if (hwc.hasClientComposition(mHwcDisplayId)) {
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000312#else
313 // We need to call eglSwapBuffers() if:
314 // (1) we don't have a hardware composer, or
315 // (2) we did GLES composition this frame, and either
316 // (a) we have framebuffer target support (not present on legacy
317 // devices, where HWComposer::commit() handles things); or
318 // (b) this is a virtual display
319 if (hwc.initCheck() != NO_ERROR ||
320 (hwc.hasGlesComposition(mHwcDisplayId) &&
321 (hwc.supportsFramebufferTarget() || mType >= DISPLAY_VIRTUAL))) {
322#endif
Jesse Hall99c7dbb2013-03-14 14:29:29 -0700323 EGLBoolean success = eglSwapBuffers(mDisplay, mSurface);
324 if (!success) {
325 EGLint error = eglGetError();
326 if (error == EGL_CONTEXT_LOST ||
327 mType == DisplayDevice::DISPLAY_PRIMARY) {
328 LOG_ALWAYS_FATAL("eglSwapBuffers(%p, %p) failed with 0x%08x",
329 mDisplay, mSurface, error);
330 } else {
331 ALOGE("eglSwapBuffers(%p, %p) failed with 0x%08x",
332 mDisplay, mSurface, error);
Mathias Agopianda27af92012-09-13 18:17:13 -0700333 }
334 }
335 }
Mathias Agopian52e21482012-09-24 18:07:21 -0700336
Jesse Hall99c7dbb2013-03-14 14:29:29 -0700337 status_t result = mDisplaySurface->advanceFrame();
338 if (result != NO_ERROR) {
339 ALOGE("[%s] failed pushing new frame to HWC: %d",
340 mDisplayName.string(), result);
Mathias Agopian32341382012-09-25 19:16:28 -0700341 }
Mathias Agopianda27af92012-09-13 18:17:13 -0700342}
343
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000344#ifdef USE_HWC2
Dan Stoza9e56aa02015-11-02 13:00:03 -0800345void DisplayDevice::onSwapBuffersCompleted() const {
346 mDisplaySurface->onFrameCommitted();
347}
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000348#else
349void DisplayDevice::onSwapBuffersCompleted(HWComposer& hwc) const {
350 if (hwc.initCheck() == NO_ERROR) {
351 mDisplaySurface->onFrameCommitted();
352 }
353}
354#endif
Mathias Agopianda27af92012-09-13 18:17:13 -0700355
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700356uint32_t DisplayDevice::getFlags() const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800357{
358 return mFlags;
359}
360
Mathias Agopian875d8e12013-06-07 15:35:48 -0700361EGLBoolean DisplayDevice::makeCurrent(EGLDisplay dpy, EGLContext ctx) const {
Mathias Agopianda8d0a52012-09-04 15:05:38 -0700362 EGLBoolean result = EGL_TRUE;
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700363 EGLSurface sur = eglGetCurrentSurface(EGL_DRAW);
Mathias Agopian875d8e12013-06-07 15:35:48 -0700364 if (sur != mSurface) {
365 result = eglMakeCurrent(dpy, mSurface, mSurface, ctx);
Mathias Agopianda8d0a52012-09-04 15:05:38 -0700366 if (result == EGL_TRUE) {
Jesse Hallf460f552013-08-06 17:08:53 -0700367 if (mType >= DisplayDevice::DISPLAY_VIRTUAL)
368 eglSwapInterval(dpy, 0);
Mathias Agopianda8d0a52012-09-04 15:05:38 -0700369 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700370 }
Mathias Agopian931bda12013-08-28 18:11:46 -0700371 setViewportAndProjection();
Mathias Agopianda8d0a52012-09-04 15:05:38 -0700372 return result;
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700373}
374
Mathias Agopian875d8e12013-06-07 15:35:48 -0700375void DisplayDevice::setViewportAndProjection() const {
376 size_t w = mDisplayWidth;
377 size_t h = mDisplayHeight;
Dan Stozac1879002014-05-22 15:59:05 -0700378 Rect sourceCrop(0, 0, w, h);
Riley Andrewsc3ebe662014-09-04 16:20:31 -0700379 mFlinger->getRenderEngine().setViewportAndProjection(w, h, sourceCrop, h,
380 false, Transform::ROT_0);
Mathias Agopianbae92d02012-09-28 01:00:47 -0700381}
382
Dan Stoza9e56aa02015-11-02 13:00:03 -0800383const sp<Fence>& DisplayDevice::getClientTargetAcquireFence() const {
384 return mDisplaySurface->getClientTargetAcquireFence();
385}
Dan Stoza9e56aa02015-11-02 13:00:03 -0800386
Mathias Agopian1b031492012-06-20 17:51:20 -0700387// ----------------------------------------------------------------------------
388
Mathias Agopian13127d82013-03-05 17:47:11 -0800389void DisplayDevice::setVisibleLayersSortedByZ(const Vector< sp<Layer> >& layers) {
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700390 mVisibleLayersSortedByZ = layers;
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700391}
392
Mathias Agopian13127d82013-03-05 17:47:11 -0800393const Vector< sp<Layer> >& DisplayDevice::getVisibleLayersSortedByZ() const {
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700394 return mVisibleLayersSortedByZ;
395}
396
Mathias Agopiancd60f992012-08-16 16:28:27 -0700397Region DisplayDevice::getDirtyRegion(bool repaintEverything) const {
398 Region dirty;
Mathias Agopiancd60f992012-08-16 16:28:27 -0700399 if (repaintEverything) {
400 dirty.set(getBounds());
401 } else {
Mathias Agopianda8d0a52012-09-04 15:05:38 -0700402 const Transform& planeTransform(mGlobalTransform);
Mathias Agopiancd60f992012-08-16 16:28:27 -0700403 dirty = planeTransform.transform(this->dirtyRegion);
404 dirty.andSelf(getBounds());
405 }
406 return dirty;
407}
408
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700409// ----------------------------------------------------------------------------
Prashant Malani2c9b11f2014-05-25 01:36:31 -0700410void DisplayDevice::setPowerMode(int mode) {
411 mPowerMode = mode;
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700412}
413
Prashant Malani2c9b11f2014-05-25 01:36:31 -0700414int DisplayDevice::getPowerMode() const {
415 return mPowerMode;
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700416}
417
Prashant Malani2c9b11f2014-05-25 01:36:31 -0700418bool DisplayDevice::isDisplayOn() const {
419 return (mPowerMode != HWC_POWER_MODE_OFF);
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700420}
421
422// ----------------------------------------------------------------------------
Michael Lentine6c9e34a2014-07-14 13:48:55 -0700423void DisplayDevice::setActiveConfig(int mode) {
424 mActiveConfig = mode;
425}
426
427int DisplayDevice::getActiveConfig() const {
428 return mActiveConfig;
429}
430
431// ----------------------------------------------------------------------------
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000432#ifdef USE_HWC2
Michael Wright28f24d02016-07-12 13:30:53 -0700433void DisplayDevice::setActiveColorMode(android_color_mode_t mode) {
434 mActiveColorMode = mode;
435}
436
437android_color_mode_t DisplayDevice::getActiveColorMode() const {
438 return mActiveColorMode;
439}
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000440#endif
Michael Wright28f24d02016-07-12 13:30:53 -0700441
442// ----------------------------------------------------------------------------
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700443
Mathias Agopian28947d72012-08-08 18:51:15 -0700444void DisplayDevice::setLayerStack(uint32_t stack) {
445 mLayerStack = stack;
446 dirtyRegion.set(bounds());
447}
448
449// ----------------------------------------------------------------------------
450
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700451uint32_t DisplayDevice::getOrientationTransform() const {
452 uint32_t transform = 0;
453 switch (mOrientation) {
454 case DisplayState::eOrientationDefault:
455 transform = Transform::ROT_0;
456 break;
457 case DisplayState::eOrientation90:
458 transform = Transform::ROT_90;
459 break;
460 case DisplayState::eOrientation180:
461 transform = Transform::ROT_180;
462 break;
463 case DisplayState::eOrientation270:
464 transform = Transform::ROT_270;
465 break;
466 }
467 return transform;
468}
469
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700470status_t DisplayDevice::orientationToTransfrom(
Mathias Agopian1b031492012-06-20 17:51:20 -0700471 int orientation, int w, int h, Transform* tr)
472{
473 uint32_t flags = 0;
474 switch (orientation) {
Mathias Agopian3165cc22012-08-08 19:42:09 -0700475 case DisplayState::eOrientationDefault:
Mathias Agopian1b031492012-06-20 17:51:20 -0700476 flags = Transform::ROT_0;
477 break;
Mathias Agopian3165cc22012-08-08 19:42:09 -0700478 case DisplayState::eOrientation90:
Mathias Agopian1b031492012-06-20 17:51:20 -0700479 flags = Transform::ROT_90;
480 break;
Mathias Agopian3165cc22012-08-08 19:42:09 -0700481 case DisplayState::eOrientation180:
Mathias Agopian1b031492012-06-20 17:51:20 -0700482 flags = Transform::ROT_180;
483 break;
Mathias Agopian3165cc22012-08-08 19:42:09 -0700484 case DisplayState::eOrientation270:
Mathias Agopian1b031492012-06-20 17:51:20 -0700485 flags = Transform::ROT_270;
486 break;
487 default:
488 return BAD_VALUE;
489 }
490 tr->set(flags, w, h);
491 return NO_ERROR;
492}
493
Michael Lentine47e45402014-07-18 15:34:25 -0700494void DisplayDevice::setDisplaySize(const int newWidth, const int newHeight) {
495 dirtyRegion.set(getBounds());
496
Michael Lentinef2568de2014-08-20 10:51:23 -0700497 if (mSurface != EGL_NO_SURFACE) {
498 eglDestroySurface(mDisplay, mSurface);
499 mSurface = EGL_NO_SURFACE;
500 }
501
Michael Lentine47e45402014-07-18 15:34:25 -0700502 mDisplaySurface->resizeBuffers(newWidth, newHeight);
503
504 ANativeWindow* const window = mNativeWindow.get();
505 mSurface = eglCreateWindowSurface(mDisplay, mConfig, window, NULL);
506 eglQuerySurface(mDisplay, mSurface, EGL_WIDTH, &mDisplayWidth);
507 eglQuerySurface(mDisplay, mSurface, EGL_HEIGHT, &mDisplayHeight);
508
509 LOG_FATAL_IF(mDisplayWidth != newWidth,
510 "Unable to set new width to %d", newWidth);
511 LOG_FATAL_IF(mDisplayHeight != newHeight,
512 "Unable to set new height to %d", newHeight);
513}
514
Mathias Agopian00e8c7a2012-09-04 19:30:46 -0700515void DisplayDevice::setProjection(int orientation,
Mathias Agopianf5f714a2013-02-26 16:54:05 -0800516 const Rect& newViewport, const Rect& newFrame) {
517 Rect viewport(newViewport);
518 Rect frame(newFrame);
519
520 const int w = mDisplayWidth;
521 const int h = mDisplayHeight;
522
523 Transform R;
524 DisplayDevice::orientationToTransfrom(orientation, w, h, &R);
525
526 if (!frame.isValid()) {
527 // the destination frame can be invalid if it has never been set,
528 // in that case we assume the whole display frame.
529 frame = Rect(w, h);
530 }
531
532 if (viewport.isEmpty()) {
533 // viewport can be invalid if it has never been set, in that case
534 // we assume the whole display size.
535 // it's also invalid to have an empty viewport, so we handle that
536 // case in the same way.
537 viewport = Rect(w, h);
538 if (R.getOrientation() & Transform::ROT_90) {
539 // viewport is always specified in the logical orientation
540 // of the display (ie: post-rotation).
541 swap(viewport.right, viewport.bottom);
542 }
543 }
544
545 dirtyRegion.set(getBounds());
546
547 Transform TL, TP, S;
548 float src_width = viewport.width();
549 float src_height = viewport.height();
550 float dst_width = frame.width();
551 float dst_height = frame.height();
552 if (src_width != dst_width || src_height != dst_height) {
553 float sx = dst_width / src_width;
554 float sy = dst_height / src_height;
555 S.set(sx, 0, 0, sy);
556 }
557
558 float src_x = viewport.left;
559 float src_y = viewport.top;
560 float dst_x = frame.left;
561 float dst_y = frame.top;
562 TL.set(-src_x, -src_y);
563 TP.set(dst_x, dst_y);
564
565 // The viewport and frame are both in the logical orientation.
566 // Apply the logical translation, scale to physical size, apply the
567 // physical translation and finally rotate to the physical orientation.
568 mGlobalTransform = R * TP * S * TL;
569
570 const uint8_t type = mGlobalTransform.getType();
571 mNeedsFiltering = (!mGlobalTransform.preserveRects() ||
572 (type >= Transform::SCALE));
573
574 mScissor = mGlobalTransform.transform(viewport);
575 if (mScissor.isEmpty()) {
Mathias Agopian6c7f25a2013-05-09 20:37:10 -0700576 mScissor = getBounds();
Mathias Agopianf5f714a2013-02-26 16:54:05 -0800577 }
578
Mathias Agopianda8d0a52012-09-04 15:05:38 -0700579 mOrientation = orientation;
Pablo Ceballos021623b2016-04-15 17:31:51 -0700580 if (mType == DisplayType::DISPLAY_PRIMARY) {
581 uint32_t transform = 0;
582 switch (mOrientation) {
583 case DisplayState::eOrientationDefault:
584 transform = Transform::ROT_0;
585 break;
586 case DisplayState::eOrientation90:
587 transform = Transform::ROT_90;
588 break;
589 case DisplayState::eOrientation180:
590 transform = Transform::ROT_180;
591 break;
592 case DisplayState::eOrientation270:
593 transform = Transform::ROT_270;
594 break;
595 }
596 sPrimaryDisplayOrientation = transform;
597 }
Mathias Agopian00e8c7a2012-09-04 19:30:46 -0700598 mViewport = viewport;
599 mFrame = frame;
Mathias Agopian1b031492012-06-20 17:51:20 -0700600}
Mathias Agopian1d12d8a2012-09-18 01:38:00 -0700601
Pablo Ceballos021623b2016-04-15 17:31:51 -0700602uint32_t DisplayDevice::getPrimaryDisplayOrientationTransform() {
603 return sPrimaryDisplayOrientation;
604}
605
Mathias Agopian74d211a2013-04-22 16:55:35 +0200606void DisplayDevice::dump(String8& result) const {
Mathias Agopian1d12d8a2012-09-18 01:38:00 -0700607 const Transform& tr(mGlobalTransform);
Mathias Agopian74d211a2013-04-22 16:55:35 +0200608 result.appendFormat(
Mathias Agopian1d12d8a2012-09-18 01:38:00 -0700609 "+ DisplayDevice: %s\n"
Jesse Hall02d86562013-03-25 14:43:23 -0700610 " type=%x, hwcId=%d, layerStack=%u, (%4dx%4d), ANativeWindow=%p, orient=%2d (type=%08x), "
Pablo Ceballosb5b35632016-02-23 11:18:51 -0800611 "flips=%u, isSecure=%d, powerMode=%d, activeConfig=%d, numLayers=%zu\n"
Mathias Agopian766dc492012-10-30 18:08:06 -0700612 " v:[%d,%d,%d,%d], f:[%d,%d,%d,%d], s:[%d,%d,%d,%d],"
Mathias Agopian1d12d8a2012-09-18 01:38:00 -0700613 "transform:[[%0.3f,%0.3f,%0.3f][%0.3f,%0.3f,%0.3f][%0.3f,%0.3f,%0.3f]]\n",
Jesse Hall02d86562013-03-25 14:43:23 -0700614 mDisplayName.string(), mType, mHwcDisplayId,
Mathias Agopian1d12d8a2012-09-18 01:38:00 -0700615 mLayerStack, mDisplayWidth, mDisplayHeight, mNativeWindow.get(),
616 mOrientation, tr.getType(), getPageFlipCount(),
Pablo Ceballosb5b35632016-02-23 11:18:51 -0800617 mIsSecure, mPowerMode, mActiveConfig,
Michael Lentine6c9e34a2014-07-14 13:48:55 -0700618 mVisibleLayersSortedByZ.size(),
Mathias Agopian1d12d8a2012-09-18 01:38:00 -0700619 mViewport.left, mViewport.top, mViewport.right, mViewport.bottom,
620 mFrame.left, mFrame.top, mFrame.right, mFrame.bottom,
Mathias Agopian766dc492012-10-30 18:08:06 -0700621 mScissor.left, mScissor.top, mScissor.right, mScissor.bottom,
Mathias Agopian1d12d8a2012-09-18 01:38:00 -0700622 tr[0][0], tr[1][0], tr[2][0],
623 tr[0][1], tr[1][1], tr[2][1],
624 tr[0][2], tr[1][2], tr[2][2]);
625
Jesse Hall99c7dbb2013-03-14 14:29:29 -0700626 String8 surfaceDump;
Dan Stozaf10c46e2014-11-11 10:32:31 -0800627 mDisplaySurface->dumpAsString(surfaceDump);
Jesse Hall99c7dbb2013-03-14 14:29:29 -0700628 result.append(surfaceDump);
Mathias Agopian1d12d8a2012-09-18 01:38:00 -0700629}
Irvelffc9efc2016-07-27 15:16:37 -0700630
631std::atomic<int32_t> DisplayDeviceState::nextDisplayId(1);
632
633DisplayDeviceState::DisplayDeviceState(DisplayDevice::DisplayType type, bool isSecure)
634 : type(type),
635 layerStack(DisplayDevice::NO_LAYER_STACK),
636 orientation(0),
637 width(0),
638 height(0),
639 isSecure(isSecure)
640{
641 viewport.makeInvalid();
642 frame.makeInvalid();
643}