blob: c3b48caf7bd571af547a71921962ebc5901f18ec [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,
68 &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
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -070083DisplayDevice::DisplayDevice(
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080084 const sp<SurfaceFlinger>& flinger,
Jamie Gennisdd3cb842012-10-19 18:19:11 -070085 DisplayType type,
Jesse Hallffe1f192013-03-22 15:13:48 -070086 int32_t hwcId,
Fabien Sanglard9d96de42016-10-11 00:15:18 +000087#ifndef USE_HWC2
88 int format,
89#endif
Jamie Gennisdd3cb842012-10-19 18:19:11 -070090 bool isSecure,
91 const wp<IBinder>& displayToken,
Jesse Hall99c7dbb2013-03-14 14:29:29 -070092 const sp<DisplaySurface>& displaySurface,
Mathias Agopiandb89edc2013-08-02 01:40:18 -070093 const sp<IGraphicBufferProducer>& producer,
Mathias Agopiana4912602012-07-12 14:25:33 -070094 EGLConfig config)
Jesse Hallb7a05492014-08-14 15:45:06 -070095 : lastCompositionHadVisibleLayers(false),
96 mFlinger(flinger),
Dan Stoza9e56aa02015-11-02 13:00:03 -080097 mType(type),
98 mHwcDisplayId(hwcId),
Chih-Wei Huang27e25622013-01-07 17:33:56 +080099 mDisplayToken(displayToken),
Jesse Hall99c7dbb2013-03-14 14:29:29 -0700100 mDisplaySurface(displaySurface),
Mathias Agopian92a979a2012-08-02 18:32:23 -0700101 mDisplay(EGL_NO_DISPLAY),
102 mSurface(EGL_NO_SURFACE),
Dan Stoza9e56aa02015-11-02 13:00:03 -0800103 mDisplayWidth(),
104 mDisplayHeight(),
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000105#ifndef USE_HWC2
106 mFormat(),
107#endif
Mathias Agopian92a979a2012-08-02 18:32:23 -0700108 mFlags(),
109 mPageFlipCount(),
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700110 mIsSecure(isSecure),
Jesse Hall01e29052013-02-19 16:13:35 -0800111 mLayerStack(NO_LAYER_STACK),
Prashant Malani2c9b11f2014-05-25 01:36:31 -0700112 mOrientation(),
Michael Lentine6c9e34a2014-07-14 13:48:55 -0700113 mPowerMode(HWC_POWER_MODE_OFF),
114 mActiveConfig(0)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800115{
james.zhang5e8eb5e2015-12-01 17:55:11 +0800116 Surface* surface;
117 mNativeWindow = surface = new Surface(producer, false);
Jesse Hallffe1f192013-03-22 15:13:48 -0700118 ANativeWindow* const window = mNativeWindow.get();
119
Courtney Goeltzenleuchter62caf7c2017-03-14 14:18:28 -0600120#ifdef USE_HWC2
121 mActiveColorMode = static_cast<android_color_mode_t>(-1);
122#endif
Jesse Hallffe1f192013-03-22 15:13:48 -0700123 /*
124 * Create our display's surface
125 */
126
james.zhang5e8eb5e2015-12-01 17:55:11 +0800127 EGLSurface eglSurface;
Jesse Hallffe1f192013-03-22 15:13:48 -0700128 EGLDisplay display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
Jesse Hall19e87292013-12-23 21:02:15 -0800129 if (config == EGL_NO_CONFIG) {
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000130#ifdef USE_HWC2
Dan Stoza9e56aa02015-11-02 13:00:03 -0800131 config = RenderEngine::chooseEglConfig(display, PIXEL_FORMAT_RGBA_8888);
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000132#else
133 config = RenderEngine::chooseEglConfig(display, format);
134#endif
Jesse Hall19e87292013-12-23 21:02:15 -0800135 }
james.zhang5e8eb5e2015-12-01 17:55:11 +0800136 eglSurface = eglCreateWindowSurface(display, config, window, NULL);
137 eglQuerySurface(display, eglSurface, EGL_WIDTH, &mDisplayWidth);
138 eglQuerySurface(display, eglSurface, EGL_HEIGHT, &mDisplayHeight);
Jesse Hallffe1f192013-03-22 15:13:48 -0700139
John Dong4ee56962014-02-21 12:37:59 -0800140 // Make sure that composition can never be stalled by a virtual display
141 // consumer that isn't processing buffers fast enough. We have to do this
142 // in two places:
143 // * Here, in case the display is composed entirely by HWC.
144 // * In makeCurrent(), using eglSwapInterval. Some EGL drivers set the
145 // window's swap interval in eglMakeCurrent, so they'll override the
146 // interval we set here.
147 if (mType >= DisplayDevice::DISPLAY_VIRTUAL)
148 window->setSwapInterval(window, 0);
149
Michael Lentine47e45402014-07-18 15:34:25 -0700150 mConfig = config;
Jesse Hallffe1f192013-03-22 15:13:48 -0700151 mDisplay = display;
james.zhang5e8eb5e2015-12-01 17:55:11 +0800152 mSurface = eglSurface;
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000153#ifndef USE_HWC2
154 mFormat = format;
155#endif
Jesse Hallffe1f192013-03-22 15:13:48 -0700156 mPageFlipCount = 0;
157 mViewport.makeInvalid();
158 mFrame.makeInvalid();
159
160 // virtual displays are always considered enabled
Prashant Malani2c9b11f2014-05-25 01:36:31 -0700161 mPowerMode = (mType >= DisplayDevice::DISPLAY_VIRTUAL) ?
162 HWC_POWER_MODE_NORMAL : HWC_POWER_MODE_OFF;
Jesse Hallffe1f192013-03-22 15:13:48 -0700163
164 // Name the display. The name will be replaced shortly if the display
165 // was created with createDisplay().
166 switch (mType) {
167 case DISPLAY_PRIMARY:
168 mDisplayName = "Built-in Screen";
169 break;
170 case DISPLAY_EXTERNAL:
171 mDisplayName = "HDMI Screen";
172 break;
173 default:
174 mDisplayName = "Virtual Screen"; // e.g. Overlay #n
175 break;
176 }
177
178 // initialize the display orientation transform.
179 setProjection(DisplayState::eOrientationDefault, mViewport, mFrame);
james.zhang5e8eb5e2015-12-01 17:55:11 +0800180
Jaesoo Lee720a7242017-01-31 15:26:18 +0900181 if (useTripleFramebuffer) {
182 surface->allocateBuffers();
183 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800184}
185
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700186DisplayDevice::~DisplayDevice() {
Mathias Agopian92a979a2012-08-02 18:32:23 -0700187 if (mSurface != EGL_NO_SURFACE) {
188 eglDestroySurface(mDisplay, mSurface);
189 mSurface = EGL_NO_SURFACE;
190 }
191}
192
Jesse Hall02d86562013-03-25 14:43:23 -0700193void DisplayDevice::disconnect(HWComposer& hwc) {
194 if (mHwcDisplayId >= 0) {
195 hwc.disconnectDisplay(mHwcDisplayId);
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000196#ifndef USE_HWC2
197 if (mHwcDisplayId >= DISPLAY_VIRTUAL)
198 hwc.freeDisplayId(mHwcDisplayId);
199#endif
Jesse Hall02d86562013-03-25 14:43:23 -0700200 mHwcDisplayId = -1;
201 }
202}
203
Mathias Agopian92a979a2012-08-02 18:32:23 -0700204bool DisplayDevice::isValid() const {
205 return mFlinger != NULL;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800206}
207
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700208int DisplayDevice::getWidth() const {
Mathias Agopiana4912602012-07-12 14:25:33 -0700209 return mDisplayWidth;
210}
211
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700212int DisplayDevice::getHeight() const {
Mathias Agopiana4912602012-07-12 14:25:33 -0700213 return mDisplayHeight;
214}
215
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000216#ifndef USE_HWC2
217PixelFormat DisplayDevice::getFormat() const {
218 return mFormat;
219}
220#endif
221
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700222EGLSurface DisplayDevice::getEGLSurface() const {
Mathias Agopiana4912602012-07-12 14:25:33 -0700223 return mSurface;
224}
225
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700226void DisplayDevice::setDisplayName(const String8& displayName) {
227 if (!displayName.isEmpty()) {
228 // never override the name with an empty name
229 mDisplayName = displayName;
230 }
231}
232
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700233uint32_t DisplayDevice::getPageFlipCount() const {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700234 return mPageFlipCount;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800235}
236
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000237#ifndef USE_HWC2
238status_t DisplayDevice::compositionComplete() const {
239 return mDisplaySurface->compositionComplete();
240}
241#endif
242
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700243void DisplayDevice::flip(const Region& dirty) const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800244{
Mathias Agopian875d8e12013-06-07 15:35:48 -0700245 mFlinger->getRenderEngine().checkErrors();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800246
Andreas Gampe89fd4f72014-11-13 14:18:56 -0800247 if (kEGLAndroidSwapRectangle) {
248 if (mFlags & SWAP_RECTANGLE) {
249 const Region newDirty(dirty.intersect(bounds()));
250 const Rect b(newDirty.getBounds());
251 eglSetSwapRectangleANDROID(mDisplay, mSurface,
252 b.left, b.top, b.width(), b.height());
253 }
Jesse Hall01e29052013-02-19 16:13:35 -0800254 }
Mathias Agopiand8707032012-09-18 01:21:55 -0700255
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700256 mPageFlipCount++;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800257}
258
Dan Stoza71433162014-02-04 16:22:36 -0800259status_t DisplayDevice::beginFrame(bool mustRecompose) const {
260 return mDisplaySurface->beginFrame(mustRecompose);
Jesse Hall028dc8f2013-08-20 16:35:32 -0700261}
262
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000263#ifdef USE_HWC2
Dan Stoza9e56aa02015-11-02 13:00:03 -0800264status_t DisplayDevice::prepareFrame(HWComposer& hwc) {
265 status_t error = hwc.prepare(*this);
266 if (error != NO_ERROR) {
267 return error;
268 }
269
270 DisplaySurface::CompositionType compositionType;
271 bool hasClient = hwc.hasClientComposition(mHwcDisplayId);
272 bool hasDevice = hwc.hasDeviceComposition(mHwcDisplayId);
273 if (hasClient && hasDevice) {
274 compositionType = DisplaySurface::COMPOSITION_MIXED;
275 } else if (hasClient) {
276 compositionType = DisplaySurface::COMPOSITION_GLES;
277 } else if (hasDevice) {
278 compositionType = DisplaySurface::COMPOSITION_HWC;
279 } else {
280 // Nothing to do -- when turning the screen off we get a frame like
281 // this. Call it a HWC frame since we won't be doing any GLES work but
282 // will do a prepare/set cycle.
283 compositionType = DisplaySurface::COMPOSITION_HWC;
284 }
285 return mDisplaySurface->prepareFrame(compositionType);
286}
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000287#else
288status_t DisplayDevice::prepareFrame(const HWComposer& hwc) const {
289 DisplaySurface::CompositionType compositionType;
290 bool haveGles = hwc.hasGlesComposition(mHwcDisplayId);
291 bool haveHwc = hwc.hasHwcComposition(mHwcDisplayId);
292 if (haveGles && haveHwc) {
293 compositionType = DisplaySurface::COMPOSITION_MIXED;
294 } else if (haveGles) {
295 compositionType = DisplaySurface::COMPOSITION_GLES;
296 } else if (haveHwc) {
297 compositionType = DisplaySurface::COMPOSITION_HWC;
298 } else {
299 // Nothing to do -- when turning the screen off we get a frame like
300 // this. Call it a HWC frame since we won't be doing any GLES work but
301 // will do a prepare/set cycle.
302 compositionType = DisplaySurface::COMPOSITION_HWC;
303 }
304 return mDisplaySurface->prepareFrame(compositionType);
305}
306#endif
Jesse Hall38efe862013-04-06 23:12:29 -0700307
Mathias Agopianda27af92012-09-13 18:17:13 -0700308void DisplayDevice::swapBuffers(HWComposer& hwc) const {
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000309#ifdef USE_HWC2
Dan Stoza9e56aa02015-11-02 13:00:03 -0800310 if (hwc.hasClientComposition(mHwcDisplayId)) {
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000311#else
312 // We need to call eglSwapBuffers() if:
313 // (1) we don't have a hardware composer, or
314 // (2) we did GLES composition this frame, and either
315 // (a) we have framebuffer target support (not present on legacy
316 // devices, where HWComposer::commit() handles things); or
317 // (b) this is a virtual display
318 if (hwc.initCheck() != NO_ERROR ||
319 (hwc.hasGlesComposition(mHwcDisplayId) &&
320 (hwc.supportsFramebufferTarget() || mType >= DISPLAY_VIRTUAL))) {
321#endif
Jesse Hall99c7dbb2013-03-14 14:29:29 -0700322 EGLBoolean success = eglSwapBuffers(mDisplay, mSurface);
323 if (!success) {
324 EGLint error = eglGetError();
325 if (error == EGL_CONTEXT_LOST ||
326 mType == DisplayDevice::DISPLAY_PRIMARY) {
327 LOG_ALWAYS_FATAL("eglSwapBuffers(%p, %p) failed with 0x%08x",
328 mDisplay, mSurface, error);
329 } else {
330 ALOGE("eglSwapBuffers(%p, %p) failed with 0x%08x",
331 mDisplay, mSurface, error);
Mathias Agopianda27af92012-09-13 18:17:13 -0700332 }
333 }
334 }
Mathias Agopian52e21482012-09-24 18:07:21 -0700335
Jesse Hall99c7dbb2013-03-14 14:29:29 -0700336 status_t result = mDisplaySurface->advanceFrame();
337 if (result != NO_ERROR) {
338 ALOGE("[%s] failed pushing new frame to HWC: %d",
339 mDisplayName.string(), result);
Mathias Agopian32341382012-09-25 19:16:28 -0700340 }
Mathias Agopianda27af92012-09-13 18:17:13 -0700341}
342
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000343#ifdef USE_HWC2
Dan Stoza9e56aa02015-11-02 13:00:03 -0800344void DisplayDevice::onSwapBuffersCompleted() const {
345 mDisplaySurface->onFrameCommitted();
346}
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000347#else
348void DisplayDevice::onSwapBuffersCompleted(HWComposer& hwc) const {
349 if (hwc.initCheck() == NO_ERROR) {
350 mDisplaySurface->onFrameCommitted();
351 }
352}
353#endif
Mathias Agopianda27af92012-09-13 18:17:13 -0700354
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700355uint32_t DisplayDevice::getFlags() const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800356{
357 return mFlags;
358}
359
Mathias Agopian875d8e12013-06-07 15:35:48 -0700360EGLBoolean DisplayDevice::makeCurrent(EGLDisplay dpy, EGLContext ctx) const {
Mathias Agopianda8d0a52012-09-04 15:05:38 -0700361 EGLBoolean result = EGL_TRUE;
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700362 EGLSurface sur = eglGetCurrentSurface(EGL_DRAW);
Mathias Agopian875d8e12013-06-07 15:35:48 -0700363 if (sur != mSurface) {
364 result = eglMakeCurrent(dpy, mSurface, mSurface, ctx);
Mathias Agopianda8d0a52012-09-04 15:05:38 -0700365 if (result == EGL_TRUE) {
Jesse Hallf460f552013-08-06 17:08:53 -0700366 if (mType >= DisplayDevice::DISPLAY_VIRTUAL)
367 eglSwapInterval(dpy, 0);
Mathias Agopianda8d0a52012-09-04 15:05:38 -0700368 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700369 }
Mathias Agopian931bda12013-08-28 18:11:46 -0700370 setViewportAndProjection();
Mathias Agopianda8d0a52012-09-04 15:05:38 -0700371 return result;
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700372}
373
Mathias Agopian875d8e12013-06-07 15:35:48 -0700374void DisplayDevice::setViewportAndProjection() const {
375 size_t w = mDisplayWidth;
376 size_t h = mDisplayHeight;
Dan Stozac1879002014-05-22 15:59:05 -0700377 Rect sourceCrop(0, 0, w, h);
Riley Andrewsc3ebe662014-09-04 16:20:31 -0700378 mFlinger->getRenderEngine().setViewportAndProjection(w, h, sourceCrop, h,
379 false, Transform::ROT_0);
Mathias Agopianbae92d02012-09-28 01:00:47 -0700380}
381
Dan Stoza9e56aa02015-11-02 13:00:03 -0800382const sp<Fence>& DisplayDevice::getClientTargetAcquireFence() const {
383 return mDisplaySurface->getClientTargetAcquireFence();
384}
Dan Stoza9e56aa02015-11-02 13:00:03 -0800385
Mathias Agopian1b031492012-06-20 17:51:20 -0700386// ----------------------------------------------------------------------------
387
Mathias Agopian13127d82013-03-05 17:47:11 -0800388void DisplayDevice::setVisibleLayersSortedByZ(const Vector< sp<Layer> >& layers) {
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700389 mVisibleLayersSortedByZ = layers;
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700390}
391
Mathias Agopian13127d82013-03-05 17:47:11 -0800392const Vector< sp<Layer> >& DisplayDevice::getVisibleLayersSortedByZ() const {
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700393 return mVisibleLayersSortedByZ;
394}
395
Mathias Agopiancd60f992012-08-16 16:28:27 -0700396Region DisplayDevice::getDirtyRegion(bool repaintEverything) const {
397 Region dirty;
Mathias Agopiancd60f992012-08-16 16:28:27 -0700398 if (repaintEverything) {
399 dirty.set(getBounds());
400 } else {
Mathias Agopianda8d0a52012-09-04 15:05:38 -0700401 const Transform& planeTransform(mGlobalTransform);
Mathias Agopiancd60f992012-08-16 16:28:27 -0700402 dirty = planeTransform.transform(this->dirtyRegion);
403 dirty.andSelf(getBounds());
404 }
405 return dirty;
406}
407
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700408// ----------------------------------------------------------------------------
Prashant Malani2c9b11f2014-05-25 01:36:31 -0700409void DisplayDevice::setPowerMode(int mode) {
410 mPowerMode = mode;
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700411}
412
Prashant Malani2c9b11f2014-05-25 01:36:31 -0700413int DisplayDevice::getPowerMode() const {
414 return mPowerMode;
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700415}
416
Prashant Malani2c9b11f2014-05-25 01:36:31 -0700417bool DisplayDevice::isDisplayOn() const {
418 return (mPowerMode != HWC_POWER_MODE_OFF);
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700419}
420
421// ----------------------------------------------------------------------------
Michael Lentine6c9e34a2014-07-14 13:48:55 -0700422void DisplayDevice::setActiveConfig(int mode) {
423 mActiveConfig = mode;
424}
425
426int DisplayDevice::getActiveConfig() const {
427 return mActiveConfig;
428}
429
430// ----------------------------------------------------------------------------
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000431#ifdef USE_HWC2
Michael Wright28f24d02016-07-12 13:30:53 -0700432void DisplayDevice::setActiveColorMode(android_color_mode_t mode) {
433 mActiveColorMode = mode;
434}
435
436android_color_mode_t DisplayDevice::getActiveColorMode() const {
437 return mActiveColorMode;
438}
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000439#endif
Michael Wright28f24d02016-07-12 13:30:53 -0700440
441// ----------------------------------------------------------------------------
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700442
Mathias Agopian28947d72012-08-08 18:51:15 -0700443void DisplayDevice::setLayerStack(uint32_t stack) {
444 mLayerStack = stack;
445 dirtyRegion.set(bounds());
446}
447
448// ----------------------------------------------------------------------------
449
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700450uint32_t DisplayDevice::getOrientationTransform() const {
451 uint32_t transform = 0;
452 switch (mOrientation) {
453 case DisplayState::eOrientationDefault:
454 transform = Transform::ROT_0;
455 break;
456 case DisplayState::eOrientation90:
457 transform = Transform::ROT_90;
458 break;
459 case DisplayState::eOrientation180:
460 transform = Transform::ROT_180;
461 break;
462 case DisplayState::eOrientation270:
463 transform = Transform::ROT_270;
464 break;
465 }
466 return transform;
467}
468
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700469status_t DisplayDevice::orientationToTransfrom(
Mathias Agopian1b031492012-06-20 17:51:20 -0700470 int orientation, int w, int h, Transform* tr)
471{
472 uint32_t flags = 0;
473 switch (orientation) {
Mathias Agopian3165cc22012-08-08 19:42:09 -0700474 case DisplayState::eOrientationDefault:
Mathias Agopian1b031492012-06-20 17:51:20 -0700475 flags = Transform::ROT_0;
476 break;
Mathias Agopian3165cc22012-08-08 19:42:09 -0700477 case DisplayState::eOrientation90:
Mathias Agopian1b031492012-06-20 17:51:20 -0700478 flags = Transform::ROT_90;
479 break;
Mathias Agopian3165cc22012-08-08 19:42:09 -0700480 case DisplayState::eOrientation180:
Mathias Agopian1b031492012-06-20 17:51:20 -0700481 flags = Transform::ROT_180;
482 break;
Mathias Agopian3165cc22012-08-08 19:42:09 -0700483 case DisplayState::eOrientation270:
Mathias Agopian1b031492012-06-20 17:51:20 -0700484 flags = Transform::ROT_270;
485 break;
486 default:
487 return BAD_VALUE;
488 }
489 tr->set(flags, w, h);
490 return NO_ERROR;
491}
492
Michael Lentine47e45402014-07-18 15:34:25 -0700493void DisplayDevice::setDisplaySize(const int newWidth, const int newHeight) {
494 dirtyRegion.set(getBounds());
495
Michael Lentinef2568de2014-08-20 10:51:23 -0700496 if (mSurface != EGL_NO_SURFACE) {
497 eglDestroySurface(mDisplay, mSurface);
498 mSurface = EGL_NO_SURFACE;
499 }
500
Michael Lentine47e45402014-07-18 15:34:25 -0700501 mDisplaySurface->resizeBuffers(newWidth, newHeight);
502
503 ANativeWindow* const window = mNativeWindow.get();
504 mSurface = eglCreateWindowSurface(mDisplay, mConfig, window, NULL);
505 eglQuerySurface(mDisplay, mSurface, EGL_WIDTH, &mDisplayWidth);
506 eglQuerySurface(mDisplay, mSurface, EGL_HEIGHT, &mDisplayHeight);
507
508 LOG_FATAL_IF(mDisplayWidth != newWidth,
509 "Unable to set new width to %d", newWidth);
510 LOG_FATAL_IF(mDisplayHeight != newHeight,
511 "Unable to set new height to %d", newHeight);
512}
513
Mathias Agopian00e8c7a2012-09-04 19:30:46 -0700514void DisplayDevice::setProjection(int orientation,
Mathias Agopianf5f714a2013-02-26 16:54:05 -0800515 const Rect& newViewport, const Rect& newFrame) {
516 Rect viewport(newViewport);
517 Rect frame(newFrame);
518
519 const int w = mDisplayWidth;
520 const int h = mDisplayHeight;
521
522 Transform R;
523 DisplayDevice::orientationToTransfrom(orientation, w, h, &R);
524
525 if (!frame.isValid()) {
526 // the destination frame can be invalid if it has never been set,
527 // in that case we assume the whole display frame.
528 frame = Rect(w, h);
529 }
530
531 if (viewport.isEmpty()) {
532 // viewport can be invalid if it has never been set, in that case
533 // we assume the whole display size.
534 // it's also invalid to have an empty viewport, so we handle that
535 // case in the same way.
536 viewport = Rect(w, h);
537 if (R.getOrientation() & Transform::ROT_90) {
538 // viewport is always specified in the logical orientation
539 // of the display (ie: post-rotation).
540 swap(viewport.right, viewport.bottom);
541 }
542 }
543
544 dirtyRegion.set(getBounds());
545
546 Transform TL, TP, S;
547 float src_width = viewport.width();
548 float src_height = viewport.height();
549 float dst_width = frame.width();
550 float dst_height = frame.height();
551 if (src_width != dst_width || src_height != dst_height) {
552 float sx = dst_width / src_width;
553 float sy = dst_height / src_height;
554 S.set(sx, 0, 0, sy);
555 }
556
557 float src_x = viewport.left;
558 float src_y = viewport.top;
559 float dst_x = frame.left;
560 float dst_y = frame.top;
561 TL.set(-src_x, -src_y);
562 TP.set(dst_x, dst_y);
563
564 // The viewport and frame are both in the logical orientation.
565 // Apply the logical translation, scale to physical size, apply the
566 // physical translation and finally rotate to the physical orientation.
567 mGlobalTransform = R * TP * S * TL;
568
569 const uint8_t type = mGlobalTransform.getType();
570 mNeedsFiltering = (!mGlobalTransform.preserveRects() ||
571 (type >= Transform::SCALE));
572
573 mScissor = mGlobalTransform.transform(viewport);
574 if (mScissor.isEmpty()) {
Mathias Agopian6c7f25a2013-05-09 20:37:10 -0700575 mScissor = getBounds();
Mathias Agopianf5f714a2013-02-26 16:54:05 -0800576 }
577
Mathias Agopianda8d0a52012-09-04 15:05:38 -0700578 mOrientation = orientation;
Pablo Ceballos021623b2016-04-15 17:31:51 -0700579 if (mType == DisplayType::DISPLAY_PRIMARY) {
580 uint32_t transform = 0;
581 switch (mOrientation) {
582 case DisplayState::eOrientationDefault:
583 transform = Transform::ROT_0;
584 break;
585 case DisplayState::eOrientation90:
586 transform = Transform::ROT_90;
587 break;
588 case DisplayState::eOrientation180:
589 transform = Transform::ROT_180;
590 break;
591 case DisplayState::eOrientation270:
592 transform = Transform::ROT_270;
593 break;
594 }
595 sPrimaryDisplayOrientation = transform;
596 }
Mathias Agopian00e8c7a2012-09-04 19:30:46 -0700597 mViewport = viewport;
598 mFrame = frame;
Mathias Agopian1b031492012-06-20 17:51:20 -0700599}
Mathias Agopian1d12d8a2012-09-18 01:38:00 -0700600
Pablo Ceballos021623b2016-04-15 17:31:51 -0700601uint32_t DisplayDevice::getPrimaryDisplayOrientationTransform() {
602 return sPrimaryDisplayOrientation;
603}
604
Mathias Agopian74d211a2013-04-22 16:55:35 +0200605void DisplayDevice::dump(String8& result) const {
Mathias Agopian1d12d8a2012-09-18 01:38:00 -0700606 const Transform& tr(mGlobalTransform);
Mathias Agopian74d211a2013-04-22 16:55:35 +0200607 result.appendFormat(
Mathias Agopian1d12d8a2012-09-18 01:38:00 -0700608 "+ DisplayDevice: %s\n"
Jesse Hall02d86562013-03-25 14:43:23 -0700609 " type=%x, hwcId=%d, layerStack=%u, (%4dx%4d), ANativeWindow=%p, orient=%2d (type=%08x), "
Pablo Ceballosb5b35632016-02-23 11:18:51 -0800610 "flips=%u, isSecure=%d, powerMode=%d, activeConfig=%d, numLayers=%zu\n"
Mathias Agopian766dc492012-10-30 18:08:06 -0700611 " v:[%d,%d,%d,%d], f:[%d,%d,%d,%d], s:[%d,%d,%d,%d],"
Mathias Agopian1d12d8a2012-09-18 01:38:00 -0700612 "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 -0700613 mDisplayName.string(), mType, mHwcDisplayId,
Mathias Agopian1d12d8a2012-09-18 01:38:00 -0700614 mLayerStack, mDisplayWidth, mDisplayHeight, mNativeWindow.get(),
615 mOrientation, tr.getType(), getPageFlipCount(),
Pablo Ceballosb5b35632016-02-23 11:18:51 -0800616 mIsSecure, mPowerMode, mActiveConfig,
Michael Lentine6c9e34a2014-07-14 13:48:55 -0700617 mVisibleLayersSortedByZ.size(),
Mathias Agopian1d12d8a2012-09-18 01:38:00 -0700618 mViewport.left, mViewport.top, mViewport.right, mViewport.bottom,
619 mFrame.left, mFrame.top, mFrame.right, mFrame.bottom,
Mathias Agopian766dc492012-10-30 18:08:06 -0700620 mScissor.left, mScissor.top, mScissor.right, mScissor.bottom,
Mathias Agopian1d12d8a2012-09-18 01:38:00 -0700621 tr[0][0], tr[1][0], tr[2][0],
622 tr[0][1], tr[1][1], tr[2][1],
623 tr[0][2], tr[1][2], tr[2][2]);
624
Jesse Hall99c7dbb2013-03-14 14:29:29 -0700625 String8 surfaceDump;
Dan Stozaf10c46e2014-11-11 10:32:31 -0800626 mDisplaySurface->dumpAsString(surfaceDump);
Jesse Hall99c7dbb2013-03-14 14:29:29 -0700627 result.append(surfaceDump);
Mathias Agopian1d12d8a2012-09-18 01:38:00 -0700628}
Irvelffc9efc2016-07-27 15:16:37 -0700629
630std::atomic<int32_t> DisplayDeviceState::nextDisplayId(1);
631
632DisplayDeviceState::DisplayDeviceState(DisplayDevice::DisplayType type, bool isSecure)
633 : type(type),
634 layerStack(DisplayDevice::NO_LAYER_STACK),
635 orientation(0),
636 width(0),
637 height(0),
638 isSecure(isSecure)
639{
640 viewport.makeInvalid();
641 frame.makeInvalid();
642}