blob: b5ffc60b55d1ee506c8c631f274c5bc122810bfb [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
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
124 mActiveColorMode = static_cast<android_color_mode_t>(-1);
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
Dan Stoza9e56aa02015-11-02 13:00:03 -0800137 config = RenderEngine::chooseEglConfig(display, PIXEL_FORMAT_RGBA_8888);
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000138#else
139 config = RenderEngine::chooseEglConfig(display, format);
140#endif
Jesse Hall19e87292013-12-23 21:02:15 -0800141 }
james.zhang5e8eb5e2015-12-01 17:55:11 +0800142 eglSurface = eglCreateWindowSurface(display, config, window, NULL);
143 eglQuerySurface(display, eglSurface, EGL_WIDTH, &mDisplayWidth);
144 eglQuerySurface(display, eglSurface, EGL_HEIGHT, &mDisplayHeight);
Jesse Hallffe1f192013-03-22 15:13:48 -0700145
John Dong4ee56962014-02-21 12:37:59 -0800146 // Make sure that composition can never be stalled by a virtual display
147 // consumer that isn't processing buffers fast enough. We have to do this
148 // in two places:
149 // * Here, in case the display is composed entirely by HWC.
150 // * In makeCurrent(), using eglSwapInterval. Some EGL drivers set the
151 // window's swap interval in eglMakeCurrent, so they'll override the
152 // interval we set here.
153 if (mType >= DisplayDevice::DISPLAY_VIRTUAL)
154 window->setSwapInterval(window, 0);
155
Michael Lentine47e45402014-07-18 15:34:25 -0700156 mConfig = config;
Jesse Hallffe1f192013-03-22 15:13:48 -0700157 mDisplay = display;
james.zhang5e8eb5e2015-12-01 17:55:11 +0800158 mSurface = eglSurface;
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000159#ifndef USE_HWC2
160 mFormat = format;
161#endif
Jesse Hallffe1f192013-03-22 15:13:48 -0700162 mPageFlipCount = 0;
163 mViewport.makeInvalid();
164 mFrame.makeInvalid();
165
166 // virtual displays are always considered enabled
Prashant Malani2c9b11f2014-05-25 01:36:31 -0700167 mPowerMode = (mType >= DisplayDevice::DISPLAY_VIRTUAL) ?
168 HWC_POWER_MODE_NORMAL : HWC_POWER_MODE_OFF;
Jesse Hallffe1f192013-03-22 15:13:48 -0700169
170 // Name the display. The name will be replaced shortly if the display
171 // was created with createDisplay().
172 switch (mType) {
173 case DISPLAY_PRIMARY:
174 mDisplayName = "Built-in Screen";
175 break;
176 case DISPLAY_EXTERNAL:
177 mDisplayName = "HDMI Screen";
178 break;
179 default:
180 mDisplayName = "Virtual Screen"; // e.g. Overlay #n
181 break;
182 }
183
184 // initialize the display orientation transform.
185 setProjection(DisplayState::eOrientationDefault, mViewport, mFrame);
james.zhang5e8eb5e2015-12-01 17:55:11 +0800186
Jaesoo Lee720a7242017-01-31 15:26:18 +0900187 if (useTripleFramebuffer) {
188 surface->allocateBuffers();
189 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800190}
191
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700192DisplayDevice::~DisplayDevice() {
Mathias Agopian92a979a2012-08-02 18:32:23 -0700193 if (mSurface != EGL_NO_SURFACE) {
194 eglDestroySurface(mDisplay, mSurface);
195 mSurface = EGL_NO_SURFACE;
196 }
197}
198
Jesse Hall02d86562013-03-25 14:43:23 -0700199void DisplayDevice::disconnect(HWComposer& hwc) {
200 if (mHwcDisplayId >= 0) {
201 hwc.disconnectDisplay(mHwcDisplayId);
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000202#ifndef USE_HWC2
203 if (mHwcDisplayId >= DISPLAY_VIRTUAL)
204 hwc.freeDisplayId(mHwcDisplayId);
205#endif
Jesse Hall02d86562013-03-25 14:43:23 -0700206 mHwcDisplayId = -1;
207 }
208}
209
Mathias Agopian92a979a2012-08-02 18:32:23 -0700210bool DisplayDevice::isValid() const {
211 return mFlinger != NULL;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800212}
213
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700214int DisplayDevice::getWidth() const {
Mathias Agopiana4912602012-07-12 14:25:33 -0700215 return mDisplayWidth;
216}
217
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700218int DisplayDevice::getHeight() const {
Mathias Agopiana4912602012-07-12 14:25:33 -0700219 return mDisplayHeight;
220}
221
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000222#ifndef USE_HWC2
223PixelFormat DisplayDevice::getFormat() const {
224 return mFormat;
225}
226#endif
227
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700228EGLSurface DisplayDevice::getEGLSurface() const {
Mathias Agopiana4912602012-07-12 14:25:33 -0700229 return mSurface;
230}
231
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700232void DisplayDevice::setDisplayName(const String8& displayName) {
233 if (!displayName.isEmpty()) {
234 // never override the name with an empty name
235 mDisplayName = displayName;
236 }
237}
238
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700239uint32_t DisplayDevice::getPageFlipCount() const {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700240 return mPageFlipCount;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800241}
242
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000243#ifndef USE_HWC2
244status_t DisplayDevice::compositionComplete() const {
245 return mDisplaySurface->compositionComplete();
246}
247#endif
248
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700249void DisplayDevice::flip(const Region& dirty) const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800250{
Mathias Agopian875d8e12013-06-07 15:35:48 -0700251 mFlinger->getRenderEngine().checkErrors();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800252
Andreas Gampe89fd4f72014-11-13 14:18:56 -0800253 if (kEGLAndroidSwapRectangle) {
254 if (mFlags & SWAP_RECTANGLE) {
255 const Region newDirty(dirty.intersect(bounds()));
256 const Rect b(newDirty.getBounds());
257 eglSetSwapRectangleANDROID(mDisplay, mSurface,
258 b.left, b.top, b.width(), b.height());
259 }
Jesse Hall01e29052013-02-19 16:13:35 -0800260 }
Mathias Agopiand8707032012-09-18 01:21:55 -0700261
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700262 mPageFlipCount++;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800263}
264
Dan Stoza71433162014-02-04 16:22:36 -0800265status_t DisplayDevice::beginFrame(bool mustRecompose) const {
266 return mDisplaySurface->beginFrame(mustRecompose);
Jesse Hall028dc8f2013-08-20 16:35:32 -0700267}
268
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000269#ifdef USE_HWC2
Dan Stoza9e56aa02015-11-02 13:00:03 -0800270status_t DisplayDevice::prepareFrame(HWComposer& hwc) {
271 status_t error = hwc.prepare(*this);
272 if (error != NO_ERROR) {
273 return error;
274 }
275
276 DisplaySurface::CompositionType compositionType;
277 bool hasClient = hwc.hasClientComposition(mHwcDisplayId);
278 bool hasDevice = hwc.hasDeviceComposition(mHwcDisplayId);
279 if (hasClient && hasDevice) {
280 compositionType = DisplaySurface::COMPOSITION_MIXED;
281 } else if (hasClient) {
282 compositionType = DisplaySurface::COMPOSITION_GLES;
283 } else if (hasDevice) {
284 compositionType = DisplaySurface::COMPOSITION_HWC;
285 } else {
286 // Nothing to do -- when turning the screen off we get a frame like
287 // this. Call it a HWC frame since we won't be doing any GLES work but
288 // will do a prepare/set cycle.
289 compositionType = DisplaySurface::COMPOSITION_HWC;
290 }
291 return mDisplaySurface->prepareFrame(compositionType);
292}
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000293#else
294status_t DisplayDevice::prepareFrame(const HWComposer& hwc) const {
295 DisplaySurface::CompositionType compositionType;
296 bool haveGles = hwc.hasGlesComposition(mHwcDisplayId);
297 bool haveHwc = hwc.hasHwcComposition(mHwcDisplayId);
298 if (haveGles && haveHwc) {
299 compositionType = DisplaySurface::COMPOSITION_MIXED;
300 } else if (haveGles) {
301 compositionType = DisplaySurface::COMPOSITION_GLES;
302 } else if (haveHwc) {
303 compositionType = DisplaySurface::COMPOSITION_HWC;
304 } else {
305 // Nothing to do -- when turning the screen off we get a frame like
306 // this. Call it a HWC frame since we won't be doing any GLES work but
307 // will do a prepare/set cycle.
308 compositionType = DisplaySurface::COMPOSITION_HWC;
309 }
310 return mDisplaySurface->prepareFrame(compositionType);
311}
312#endif
Jesse Hall38efe862013-04-06 23:12:29 -0700313
Mathias Agopianda27af92012-09-13 18:17:13 -0700314void DisplayDevice::swapBuffers(HWComposer& hwc) const {
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000315#ifdef USE_HWC2
Dan Stoza9e56aa02015-11-02 13:00:03 -0800316 if (hwc.hasClientComposition(mHwcDisplayId)) {
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000317#else
318 // We need to call eglSwapBuffers() if:
319 // (1) we don't have a hardware composer, or
320 // (2) we did GLES composition this frame, and either
321 // (a) we have framebuffer target support (not present on legacy
322 // devices, where HWComposer::commit() handles things); or
323 // (b) this is a virtual display
324 if (hwc.initCheck() != NO_ERROR ||
325 (hwc.hasGlesComposition(mHwcDisplayId) &&
326 (hwc.supportsFramebufferTarget() || mType >= DISPLAY_VIRTUAL))) {
327#endif
Jesse Hall99c7dbb2013-03-14 14:29:29 -0700328 EGLBoolean success = eglSwapBuffers(mDisplay, mSurface);
329 if (!success) {
330 EGLint error = eglGetError();
331 if (error == EGL_CONTEXT_LOST ||
332 mType == DisplayDevice::DISPLAY_PRIMARY) {
333 LOG_ALWAYS_FATAL("eglSwapBuffers(%p, %p) failed with 0x%08x",
334 mDisplay, mSurface, error);
335 } else {
336 ALOGE("eglSwapBuffers(%p, %p) failed with 0x%08x",
337 mDisplay, mSurface, error);
Mathias Agopianda27af92012-09-13 18:17:13 -0700338 }
339 }
340 }
Mathias Agopian52e21482012-09-24 18:07:21 -0700341
Jesse Hall99c7dbb2013-03-14 14:29:29 -0700342 status_t result = mDisplaySurface->advanceFrame();
343 if (result != NO_ERROR) {
344 ALOGE("[%s] failed pushing new frame to HWC: %d",
345 mDisplayName.string(), result);
Mathias Agopian32341382012-09-25 19:16:28 -0700346 }
Mathias Agopianda27af92012-09-13 18:17:13 -0700347}
348
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000349#ifdef USE_HWC2
Dan Stoza9e56aa02015-11-02 13:00:03 -0800350void DisplayDevice::onSwapBuffersCompleted() const {
351 mDisplaySurface->onFrameCommitted();
352}
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000353#else
354void DisplayDevice::onSwapBuffersCompleted(HWComposer& hwc) const {
355 if (hwc.initCheck() == NO_ERROR) {
356 mDisplaySurface->onFrameCommitted();
357 }
358}
359#endif
Mathias Agopianda27af92012-09-13 18:17:13 -0700360
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700361uint32_t DisplayDevice::getFlags() const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800362{
363 return mFlags;
364}
365
Mathias Agopian875d8e12013-06-07 15:35:48 -0700366EGLBoolean DisplayDevice::makeCurrent(EGLDisplay dpy, EGLContext ctx) const {
Mathias Agopianda8d0a52012-09-04 15:05:38 -0700367 EGLBoolean result = EGL_TRUE;
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700368 EGLSurface sur = eglGetCurrentSurface(EGL_DRAW);
Mathias Agopian875d8e12013-06-07 15:35:48 -0700369 if (sur != mSurface) {
370 result = eglMakeCurrent(dpy, mSurface, mSurface, ctx);
Mathias Agopianda8d0a52012-09-04 15:05:38 -0700371 if (result == EGL_TRUE) {
Jesse Hallf460f552013-08-06 17:08:53 -0700372 if (mType >= DisplayDevice::DISPLAY_VIRTUAL)
373 eglSwapInterval(dpy, 0);
Mathias Agopianda8d0a52012-09-04 15:05:38 -0700374 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700375 }
Mathias Agopian931bda12013-08-28 18:11:46 -0700376 setViewportAndProjection();
Mathias Agopianda8d0a52012-09-04 15:05:38 -0700377 return result;
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700378}
379
Mathias Agopian875d8e12013-06-07 15:35:48 -0700380void DisplayDevice::setViewportAndProjection() const {
381 size_t w = mDisplayWidth;
382 size_t h = mDisplayHeight;
Dan Stozac1879002014-05-22 15:59:05 -0700383 Rect sourceCrop(0, 0, w, h);
Riley Andrewsc3ebe662014-09-04 16:20:31 -0700384 mFlinger->getRenderEngine().setViewportAndProjection(w, h, sourceCrop, h,
385 false, Transform::ROT_0);
Mathias Agopianbae92d02012-09-28 01:00:47 -0700386}
387
Dan Stoza9e56aa02015-11-02 13:00:03 -0800388const sp<Fence>& DisplayDevice::getClientTargetAcquireFence() const {
389 return mDisplaySurface->getClientTargetAcquireFence();
390}
Dan Stoza9e56aa02015-11-02 13:00:03 -0800391
Mathias Agopian1b031492012-06-20 17:51:20 -0700392// ----------------------------------------------------------------------------
393
Mathias Agopian13127d82013-03-05 17:47:11 -0800394void DisplayDevice::setVisibleLayersSortedByZ(const Vector< sp<Layer> >& layers) {
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700395 mVisibleLayersSortedByZ = layers;
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700396}
397
Mathias Agopian13127d82013-03-05 17:47:11 -0800398const Vector< sp<Layer> >& DisplayDevice::getVisibleLayersSortedByZ() const {
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700399 return mVisibleLayersSortedByZ;
400}
401
Mathias Agopiancd60f992012-08-16 16:28:27 -0700402Region DisplayDevice::getDirtyRegion(bool repaintEverything) const {
403 Region dirty;
Mathias Agopiancd60f992012-08-16 16:28:27 -0700404 if (repaintEverything) {
405 dirty.set(getBounds());
406 } else {
Mathias Agopianda8d0a52012-09-04 15:05:38 -0700407 const Transform& planeTransform(mGlobalTransform);
Mathias Agopiancd60f992012-08-16 16:28:27 -0700408 dirty = planeTransform.transform(this->dirtyRegion);
409 dirty.andSelf(getBounds());
410 }
411 return dirty;
412}
413
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700414// ----------------------------------------------------------------------------
Prashant Malani2c9b11f2014-05-25 01:36:31 -0700415void DisplayDevice::setPowerMode(int mode) {
416 mPowerMode = mode;
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700417}
418
Prashant Malani2c9b11f2014-05-25 01:36:31 -0700419int DisplayDevice::getPowerMode() const {
420 return mPowerMode;
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700421}
422
Prashant Malani2c9b11f2014-05-25 01:36:31 -0700423bool DisplayDevice::isDisplayOn() const {
424 return (mPowerMode != HWC_POWER_MODE_OFF);
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700425}
426
427// ----------------------------------------------------------------------------
Michael Lentine6c9e34a2014-07-14 13:48:55 -0700428void DisplayDevice::setActiveConfig(int mode) {
429 mActiveConfig = mode;
430}
431
432int DisplayDevice::getActiveConfig() const {
433 return mActiveConfig;
434}
435
436// ----------------------------------------------------------------------------
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000437#ifdef USE_HWC2
Michael Wright28f24d02016-07-12 13:30:53 -0700438void DisplayDevice::setActiveColorMode(android_color_mode_t mode) {
439 mActiveColorMode = mode;
440}
441
442android_color_mode_t DisplayDevice::getActiveColorMode() const {
443 return mActiveColorMode;
444}
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000445#endif
Michael Wright28f24d02016-07-12 13:30:53 -0700446
447// ----------------------------------------------------------------------------
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700448
Mathias Agopian28947d72012-08-08 18:51:15 -0700449void DisplayDevice::setLayerStack(uint32_t stack) {
450 mLayerStack = stack;
451 dirtyRegion.set(bounds());
452}
453
454// ----------------------------------------------------------------------------
455
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700456uint32_t DisplayDevice::getOrientationTransform() const {
457 uint32_t transform = 0;
458 switch (mOrientation) {
459 case DisplayState::eOrientationDefault:
460 transform = Transform::ROT_0;
461 break;
462 case DisplayState::eOrientation90:
463 transform = Transform::ROT_90;
464 break;
465 case DisplayState::eOrientation180:
466 transform = Transform::ROT_180;
467 break;
468 case DisplayState::eOrientation270:
469 transform = Transform::ROT_270;
470 break;
471 }
472 return transform;
473}
474
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700475status_t DisplayDevice::orientationToTransfrom(
Mathias Agopian1b031492012-06-20 17:51:20 -0700476 int orientation, int w, int h, Transform* tr)
477{
478 uint32_t flags = 0;
479 switch (orientation) {
Mathias Agopian3165cc22012-08-08 19:42:09 -0700480 case DisplayState::eOrientationDefault:
Mathias Agopian1b031492012-06-20 17:51:20 -0700481 flags = Transform::ROT_0;
482 break;
Mathias Agopian3165cc22012-08-08 19:42:09 -0700483 case DisplayState::eOrientation90:
Mathias Agopian1b031492012-06-20 17:51:20 -0700484 flags = Transform::ROT_90;
485 break;
Mathias Agopian3165cc22012-08-08 19:42:09 -0700486 case DisplayState::eOrientation180:
Mathias Agopian1b031492012-06-20 17:51:20 -0700487 flags = Transform::ROT_180;
488 break;
Mathias Agopian3165cc22012-08-08 19:42:09 -0700489 case DisplayState::eOrientation270:
Mathias Agopian1b031492012-06-20 17:51:20 -0700490 flags = Transform::ROT_270;
491 break;
492 default:
493 return BAD_VALUE;
494 }
495 tr->set(flags, w, h);
496 return NO_ERROR;
497}
498
Michael Lentine47e45402014-07-18 15:34:25 -0700499void DisplayDevice::setDisplaySize(const int newWidth, const int newHeight) {
500 dirtyRegion.set(getBounds());
501
Michael Lentinef2568de2014-08-20 10:51:23 -0700502 if (mSurface != EGL_NO_SURFACE) {
503 eglDestroySurface(mDisplay, mSurface);
504 mSurface = EGL_NO_SURFACE;
505 }
506
Michael Lentine47e45402014-07-18 15:34:25 -0700507 mDisplaySurface->resizeBuffers(newWidth, newHeight);
508
509 ANativeWindow* const window = mNativeWindow.get();
510 mSurface = eglCreateWindowSurface(mDisplay, mConfig, window, NULL);
511 eglQuerySurface(mDisplay, mSurface, EGL_WIDTH, &mDisplayWidth);
512 eglQuerySurface(mDisplay, mSurface, EGL_HEIGHT, &mDisplayHeight);
513
514 LOG_FATAL_IF(mDisplayWidth != newWidth,
515 "Unable to set new width to %d", newWidth);
516 LOG_FATAL_IF(mDisplayHeight != newHeight,
517 "Unable to set new height to %d", newHeight);
518}
519
Mathias Agopian00e8c7a2012-09-04 19:30:46 -0700520void DisplayDevice::setProjection(int orientation,
Mathias Agopianf5f714a2013-02-26 16:54:05 -0800521 const Rect& newViewport, const Rect& newFrame) {
522 Rect viewport(newViewport);
523 Rect frame(newFrame);
524
525 const int w = mDisplayWidth;
526 const int h = mDisplayHeight;
527
528 Transform R;
529 DisplayDevice::orientationToTransfrom(orientation, w, h, &R);
530
531 if (!frame.isValid()) {
532 // the destination frame can be invalid if it has never been set,
533 // in that case we assume the whole display frame.
534 frame = Rect(w, h);
535 }
536
537 if (viewport.isEmpty()) {
538 // viewport can be invalid if it has never been set, in that case
539 // we assume the whole display size.
540 // it's also invalid to have an empty viewport, so we handle that
541 // case in the same way.
542 viewport = Rect(w, h);
543 if (R.getOrientation() & Transform::ROT_90) {
544 // viewport is always specified in the logical orientation
545 // of the display (ie: post-rotation).
546 swap(viewport.right, viewport.bottom);
547 }
548 }
549
550 dirtyRegion.set(getBounds());
551
552 Transform TL, TP, S;
553 float src_width = viewport.width();
554 float src_height = viewport.height();
555 float dst_width = frame.width();
556 float dst_height = frame.height();
557 if (src_width != dst_width || src_height != dst_height) {
558 float sx = dst_width / src_width;
559 float sy = dst_height / src_height;
560 S.set(sx, 0, 0, sy);
561 }
562
563 float src_x = viewport.left;
564 float src_y = viewport.top;
565 float dst_x = frame.left;
566 float dst_y = frame.top;
567 TL.set(-src_x, -src_y);
568 TP.set(dst_x, dst_y);
569
570 // The viewport and frame are both in the logical orientation.
571 // Apply the logical translation, scale to physical size, apply the
572 // physical translation and finally rotate to the physical orientation.
573 mGlobalTransform = R * TP * S * TL;
574
575 const uint8_t type = mGlobalTransform.getType();
576 mNeedsFiltering = (!mGlobalTransform.preserveRects() ||
577 (type >= Transform::SCALE));
578
579 mScissor = mGlobalTransform.transform(viewport);
580 if (mScissor.isEmpty()) {
Mathias Agopian6c7f25a2013-05-09 20:37:10 -0700581 mScissor = getBounds();
Mathias Agopianf5f714a2013-02-26 16:54:05 -0800582 }
583
Mathias Agopianda8d0a52012-09-04 15:05:38 -0700584 mOrientation = orientation;
Pablo Ceballos021623b2016-04-15 17:31:51 -0700585 if (mType == DisplayType::DISPLAY_PRIMARY) {
586 uint32_t transform = 0;
587 switch (mOrientation) {
588 case DisplayState::eOrientationDefault:
589 transform = Transform::ROT_0;
590 break;
591 case DisplayState::eOrientation90:
592 transform = Transform::ROT_90;
593 break;
594 case DisplayState::eOrientation180:
595 transform = Transform::ROT_180;
596 break;
597 case DisplayState::eOrientation270:
598 transform = Transform::ROT_270;
599 break;
600 }
601 sPrimaryDisplayOrientation = transform;
602 }
Mathias Agopian00e8c7a2012-09-04 19:30:46 -0700603 mViewport = viewport;
604 mFrame = frame;
Mathias Agopian1b031492012-06-20 17:51:20 -0700605}
Mathias Agopian1d12d8a2012-09-18 01:38:00 -0700606
Pablo Ceballos021623b2016-04-15 17:31:51 -0700607uint32_t DisplayDevice::getPrimaryDisplayOrientationTransform() {
608 return sPrimaryDisplayOrientation;
609}
610
Mathias Agopian74d211a2013-04-22 16:55:35 +0200611void DisplayDevice::dump(String8& result) const {
Mathias Agopian1d12d8a2012-09-18 01:38:00 -0700612 const Transform& tr(mGlobalTransform);
Mathias Agopian74d211a2013-04-22 16:55:35 +0200613 result.appendFormat(
Mathias Agopian1d12d8a2012-09-18 01:38:00 -0700614 "+ DisplayDevice: %s\n"
Jesse Hall02d86562013-03-25 14:43:23 -0700615 " type=%x, hwcId=%d, layerStack=%u, (%4dx%4d), ANativeWindow=%p, orient=%2d (type=%08x), "
Pablo Ceballosb5b35632016-02-23 11:18:51 -0800616 "flips=%u, isSecure=%d, powerMode=%d, activeConfig=%d, numLayers=%zu\n"
Mathias Agopian766dc492012-10-30 18:08:06 -0700617 " v:[%d,%d,%d,%d], f:[%d,%d,%d,%d], s:[%d,%d,%d,%d],"
Mathias Agopian1d12d8a2012-09-18 01:38:00 -0700618 "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 -0700619 mDisplayName.string(), mType, mHwcDisplayId,
Mathias Agopian1d12d8a2012-09-18 01:38:00 -0700620 mLayerStack, mDisplayWidth, mDisplayHeight, mNativeWindow.get(),
621 mOrientation, tr.getType(), getPageFlipCount(),
Pablo Ceballosb5b35632016-02-23 11:18:51 -0800622 mIsSecure, mPowerMode, mActiveConfig,
Michael Lentine6c9e34a2014-07-14 13:48:55 -0700623 mVisibleLayersSortedByZ.size(),
Mathias Agopian1d12d8a2012-09-18 01:38:00 -0700624 mViewport.left, mViewport.top, mViewport.right, mViewport.bottom,
625 mFrame.left, mFrame.top, mFrame.right, mFrame.bottom,
Mathias Agopian766dc492012-10-30 18:08:06 -0700626 mScissor.left, mScissor.top, mScissor.right, mScissor.bottom,
Mathias Agopian1d12d8a2012-09-18 01:38:00 -0700627 tr[0][0], tr[1][0], tr[2][0],
628 tr[0][1], tr[1][1], tr[2][1],
629 tr[0][2], tr[1][2], tr[2][2]);
630
Jesse Hall99c7dbb2013-03-14 14:29:29 -0700631 String8 surfaceDump;
Dan Stozaf10c46e2014-11-11 10:32:31 -0800632 mDisplaySurface->dumpAsString(surfaceDump);
Jesse Hall99c7dbb2013-03-14 14:29:29 -0700633 result.append(surfaceDump);
Mathias Agopian1d12d8a2012-09-18 01:38:00 -0700634}
Irvelffc9efc2016-07-27 15:16:37 -0700635
636std::atomic<int32_t> DisplayDeviceState::nextDisplayId(1);
637
638DisplayDeviceState::DisplayDeviceState(DisplayDevice::DisplayType type, bool isSecure)
639 : type(type),
640 layerStack(DisplayDevice::NO_LAYER_STACK),
641 orientation(0),
642 width(0),
643 height(0),
644 isSecure(isSecure)
645{
646 viewport.makeInvalid();
647 frame.makeInvalid();
648}