blob: 9af44028025eb34b11d5cc94c9227dbd2e9c2b85 [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
Jesse Hallffe1f192013-03-22 15:13:48 -0700121 /*
122 * Create our display's surface
123 */
124
james.zhang5e8eb5e2015-12-01 17:55:11 +0800125 EGLSurface eglSurface;
Jesse Hallffe1f192013-03-22 15:13:48 -0700126 EGLDisplay display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
Jesse Hall19e87292013-12-23 21:02:15 -0800127 if (config == EGL_NO_CONFIG) {
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000128#ifdef USE_HWC2
Dan Stoza9e56aa02015-11-02 13:00:03 -0800129 config = RenderEngine::chooseEglConfig(display, PIXEL_FORMAT_RGBA_8888);
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000130#else
131 config = RenderEngine::chooseEglConfig(display, format);
132#endif
Jesse Hall19e87292013-12-23 21:02:15 -0800133 }
james.zhang5e8eb5e2015-12-01 17:55:11 +0800134 eglSurface = eglCreateWindowSurface(display, config, window, NULL);
135 eglQuerySurface(display, eglSurface, EGL_WIDTH, &mDisplayWidth);
136 eglQuerySurface(display, eglSurface, EGL_HEIGHT, &mDisplayHeight);
Jesse Hallffe1f192013-03-22 15:13:48 -0700137
John Dong4ee56962014-02-21 12:37:59 -0800138 // Make sure that composition can never be stalled by a virtual display
139 // consumer that isn't processing buffers fast enough. We have to do this
140 // in two places:
141 // * Here, in case the display is composed entirely by HWC.
142 // * In makeCurrent(), using eglSwapInterval. Some EGL drivers set the
143 // window's swap interval in eglMakeCurrent, so they'll override the
144 // interval we set here.
145 if (mType >= DisplayDevice::DISPLAY_VIRTUAL)
146 window->setSwapInterval(window, 0);
147
Michael Lentine47e45402014-07-18 15:34:25 -0700148 mConfig = config;
Jesse Hallffe1f192013-03-22 15:13:48 -0700149 mDisplay = display;
james.zhang5e8eb5e2015-12-01 17:55:11 +0800150 mSurface = eglSurface;
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000151#ifndef USE_HWC2
152 mFormat = format;
153#endif
Jesse Hallffe1f192013-03-22 15:13:48 -0700154 mPageFlipCount = 0;
155 mViewport.makeInvalid();
156 mFrame.makeInvalid();
157
158 // virtual displays are always considered enabled
Prashant Malani2c9b11f2014-05-25 01:36:31 -0700159 mPowerMode = (mType >= DisplayDevice::DISPLAY_VIRTUAL) ?
160 HWC_POWER_MODE_NORMAL : HWC_POWER_MODE_OFF;
Jesse Hallffe1f192013-03-22 15:13:48 -0700161
162 // Name the display. The name will be replaced shortly if the display
163 // was created with createDisplay().
164 switch (mType) {
165 case DISPLAY_PRIMARY:
166 mDisplayName = "Built-in Screen";
167 break;
168 case DISPLAY_EXTERNAL:
169 mDisplayName = "HDMI Screen";
170 break;
171 default:
172 mDisplayName = "Virtual Screen"; // e.g. Overlay #n
173 break;
174 }
175
176 // initialize the display orientation transform.
177 setProjection(DisplayState::eOrientationDefault, mViewport, mFrame);
james.zhang5e8eb5e2015-12-01 17:55:11 +0800178
Jaesoo Lee720a7242017-01-31 15:26:18 +0900179 if (useTripleFramebuffer) {
180 surface->allocateBuffers();
181 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800182}
183
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700184DisplayDevice::~DisplayDevice() {
Mathias Agopian92a979a2012-08-02 18:32:23 -0700185 if (mSurface != EGL_NO_SURFACE) {
186 eglDestroySurface(mDisplay, mSurface);
187 mSurface = EGL_NO_SURFACE;
188 }
189}
190
Jesse Hall02d86562013-03-25 14:43:23 -0700191void DisplayDevice::disconnect(HWComposer& hwc) {
192 if (mHwcDisplayId >= 0) {
193 hwc.disconnectDisplay(mHwcDisplayId);
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000194#ifndef USE_HWC2
195 if (mHwcDisplayId >= DISPLAY_VIRTUAL)
196 hwc.freeDisplayId(mHwcDisplayId);
197#endif
Jesse Hall02d86562013-03-25 14:43:23 -0700198 mHwcDisplayId = -1;
199 }
200}
201
Mathias Agopian92a979a2012-08-02 18:32:23 -0700202bool DisplayDevice::isValid() const {
203 return mFlinger != NULL;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800204}
205
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700206int DisplayDevice::getWidth() const {
Mathias Agopiana4912602012-07-12 14:25:33 -0700207 return mDisplayWidth;
208}
209
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700210int DisplayDevice::getHeight() const {
Mathias Agopiana4912602012-07-12 14:25:33 -0700211 return mDisplayHeight;
212}
213
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000214#ifndef USE_HWC2
215PixelFormat DisplayDevice::getFormat() const {
216 return mFormat;
217}
218#endif
219
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700220EGLSurface DisplayDevice::getEGLSurface() const {
Mathias Agopiana4912602012-07-12 14:25:33 -0700221 return mSurface;
222}
223
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700224void DisplayDevice::setDisplayName(const String8& displayName) {
225 if (!displayName.isEmpty()) {
226 // never override the name with an empty name
227 mDisplayName = displayName;
228 }
229}
230
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700231uint32_t DisplayDevice::getPageFlipCount() const {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700232 return mPageFlipCount;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800233}
234
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000235#ifndef USE_HWC2
236status_t DisplayDevice::compositionComplete() const {
237 return mDisplaySurface->compositionComplete();
238}
239#endif
240
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700241void DisplayDevice::flip(const Region& dirty) const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800242{
Mathias Agopian875d8e12013-06-07 15:35:48 -0700243 mFlinger->getRenderEngine().checkErrors();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800244
Andreas Gampe89fd4f72014-11-13 14:18:56 -0800245 if (kEGLAndroidSwapRectangle) {
246 if (mFlags & SWAP_RECTANGLE) {
247 const Region newDirty(dirty.intersect(bounds()));
248 const Rect b(newDirty.getBounds());
249 eglSetSwapRectangleANDROID(mDisplay, mSurface,
250 b.left, b.top, b.width(), b.height());
251 }
Jesse Hall01e29052013-02-19 16:13:35 -0800252 }
Mathias Agopiand8707032012-09-18 01:21:55 -0700253
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700254 mPageFlipCount++;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800255}
256
Dan Stoza71433162014-02-04 16:22:36 -0800257status_t DisplayDevice::beginFrame(bool mustRecompose) const {
258 return mDisplaySurface->beginFrame(mustRecompose);
Jesse Hall028dc8f2013-08-20 16:35:32 -0700259}
260
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000261#ifdef USE_HWC2
Dan Stoza9e56aa02015-11-02 13:00:03 -0800262status_t DisplayDevice::prepareFrame(HWComposer& hwc) {
263 status_t error = hwc.prepare(*this);
264 if (error != NO_ERROR) {
265 return error;
266 }
267
268 DisplaySurface::CompositionType compositionType;
269 bool hasClient = hwc.hasClientComposition(mHwcDisplayId);
270 bool hasDevice = hwc.hasDeviceComposition(mHwcDisplayId);
271 if (hasClient && hasDevice) {
272 compositionType = DisplaySurface::COMPOSITION_MIXED;
273 } else if (hasClient) {
274 compositionType = DisplaySurface::COMPOSITION_GLES;
275 } else if (hasDevice) {
276 compositionType = DisplaySurface::COMPOSITION_HWC;
277 } else {
278 // Nothing to do -- when turning the screen off we get a frame like
279 // this. Call it a HWC frame since we won't be doing any GLES work but
280 // will do a prepare/set cycle.
281 compositionType = DisplaySurface::COMPOSITION_HWC;
282 }
283 return mDisplaySurface->prepareFrame(compositionType);
284}
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000285#else
286status_t DisplayDevice::prepareFrame(const HWComposer& hwc) const {
287 DisplaySurface::CompositionType compositionType;
288 bool haveGles = hwc.hasGlesComposition(mHwcDisplayId);
289 bool haveHwc = hwc.hasHwcComposition(mHwcDisplayId);
290 if (haveGles && haveHwc) {
291 compositionType = DisplaySurface::COMPOSITION_MIXED;
292 } else if (haveGles) {
293 compositionType = DisplaySurface::COMPOSITION_GLES;
294 } else if (haveHwc) {
295 compositionType = DisplaySurface::COMPOSITION_HWC;
296 } else {
297 // Nothing to do -- when turning the screen off we get a frame like
298 // this. Call it a HWC frame since we won't be doing any GLES work but
299 // will do a prepare/set cycle.
300 compositionType = DisplaySurface::COMPOSITION_HWC;
301 }
302 return mDisplaySurface->prepareFrame(compositionType);
303}
304#endif
Jesse Hall38efe862013-04-06 23:12:29 -0700305
Mathias Agopianda27af92012-09-13 18:17:13 -0700306void DisplayDevice::swapBuffers(HWComposer& hwc) const {
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000307#ifdef USE_HWC2
Dan Stoza9e56aa02015-11-02 13:00:03 -0800308 if (hwc.hasClientComposition(mHwcDisplayId)) {
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000309#else
310 // We need to call eglSwapBuffers() if:
311 // (1) we don't have a hardware composer, or
312 // (2) we did GLES composition this frame, and either
313 // (a) we have framebuffer target support (not present on legacy
314 // devices, where HWComposer::commit() handles things); or
315 // (b) this is a virtual display
316 if (hwc.initCheck() != NO_ERROR ||
317 (hwc.hasGlesComposition(mHwcDisplayId) &&
318 (hwc.supportsFramebufferTarget() || mType >= DISPLAY_VIRTUAL))) {
319#endif
Jesse Hall99c7dbb2013-03-14 14:29:29 -0700320 EGLBoolean success = eglSwapBuffers(mDisplay, mSurface);
321 if (!success) {
322 EGLint error = eglGetError();
323 if (error == EGL_CONTEXT_LOST ||
324 mType == DisplayDevice::DISPLAY_PRIMARY) {
325 LOG_ALWAYS_FATAL("eglSwapBuffers(%p, %p) failed with 0x%08x",
326 mDisplay, mSurface, error);
327 } else {
328 ALOGE("eglSwapBuffers(%p, %p) failed with 0x%08x",
329 mDisplay, mSurface, error);
Mathias Agopianda27af92012-09-13 18:17:13 -0700330 }
331 }
332 }
Mathias Agopian52e21482012-09-24 18:07:21 -0700333
Jesse Hall99c7dbb2013-03-14 14:29:29 -0700334 status_t result = mDisplaySurface->advanceFrame();
335 if (result != NO_ERROR) {
336 ALOGE("[%s] failed pushing new frame to HWC: %d",
337 mDisplayName.string(), result);
Mathias Agopian32341382012-09-25 19:16:28 -0700338 }
Mathias Agopianda27af92012-09-13 18:17:13 -0700339}
340
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000341#ifdef USE_HWC2
Dan Stoza9e56aa02015-11-02 13:00:03 -0800342void DisplayDevice::onSwapBuffersCompleted() const {
343 mDisplaySurface->onFrameCommitted();
344}
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000345#else
346void DisplayDevice::onSwapBuffersCompleted(HWComposer& hwc) const {
347 if (hwc.initCheck() == NO_ERROR) {
348 mDisplaySurface->onFrameCommitted();
349 }
350}
351#endif
Mathias Agopianda27af92012-09-13 18:17:13 -0700352
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700353uint32_t DisplayDevice::getFlags() const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800354{
355 return mFlags;
356}
357
Mathias Agopian875d8e12013-06-07 15:35:48 -0700358EGLBoolean DisplayDevice::makeCurrent(EGLDisplay dpy, EGLContext ctx) const {
Mathias Agopianda8d0a52012-09-04 15:05:38 -0700359 EGLBoolean result = EGL_TRUE;
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700360 EGLSurface sur = eglGetCurrentSurface(EGL_DRAW);
Mathias Agopian875d8e12013-06-07 15:35:48 -0700361 if (sur != mSurface) {
362 result = eglMakeCurrent(dpy, mSurface, mSurface, ctx);
Mathias Agopianda8d0a52012-09-04 15:05:38 -0700363 if (result == EGL_TRUE) {
Jesse Hallf460f552013-08-06 17:08:53 -0700364 if (mType >= DisplayDevice::DISPLAY_VIRTUAL)
365 eglSwapInterval(dpy, 0);
Mathias Agopianda8d0a52012-09-04 15:05:38 -0700366 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700367 }
Mathias Agopian931bda12013-08-28 18:11:46 -0700368 setViewportAndProjection();
Mathias Agopianda8d0a52012-09-04 15:05:38 -0700369 return result;
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700370}
371
Mathias Agopian875d8e12013-06-07 15:35:48 -0700372void DisplayDevice::setViewportAndProjection() const {
373 size_t w = mDisplayWidth;
374 size_t h = mDisplayHeight;
Dan Stozac1879002014-05-22 15:59:05 -0700375 Rect sourceCrop(0, 0, w, h);
Riley Andrewsc3ebe662014-09-04 16:20:31 -0700376 mFlinger->getRenderEngine().setViewportAndProjection(w, h, sourceCrop, h,
377 false, Transform::ROT_0);
Mathias Agopianbae92d02012-09-28 01:00:47 -0700378}
379
Dan Stoza9e56aa02015-11-02 13:00:03 -0800380const sp<Fence>& DisplayDevice::getClientTargetAcquireFence() const {
381 return mDisplaySurface->getClientTargetAcquireFence();
382}
Dan Stoza9e56aa02015-11-02 13:00:03 -0800383
Mathias Agopian1b031492012-06-20 17:51:20 -0700384// ----------------------------------------------------------------------------
385
Mathias Agopian13127d82013-03-05 17:47:11 -0800386void DisplayDevice::setVisibleLayersSortedByZ(const Vector< sp<Layer> >& layers) {
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700387 mVisibleLayersSortedByZ = layers;
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700388}
389
Mathias Agopian13127d82013-03-05 17:47:11 -0800390const Vector< sp<Layer> >& DisplayDevice::getVisibleLayersSortedByZ() const {
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700391 return mVisibleLayersSortedByZ;
392}
393
Mathias Agopiancd60f992012-08-16 16:28:27 -0700394Region DisplayDevice::getDirtyRegion(bool repaintEverything) const {
395 Region dirty;
Mathias Agopiancd60f992012-08-16 16:28:27 -0700396 if (repaintEverything) {
397 dirty.set(getBounds());
398 } else {
Mathias Agopianda8d0a52012-09-04 15:05:38 -0700399 const Transform& planeTransform(mGlobalTransform);
Mathias Agopiancd60f992012-08-16 16:28:27 -0700400 dirty = planeTransform.transform(this->dirtyRegion);
401 dirty.andSelf(getBounds());
402 }
403 return dirty;
404}
405
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700406// ----------------------------------------------------------------------------
Prashant Malani2c9b11f2014-05-25 01:36:31 -0700407void DisplayDevice::setPowerMode(int mode) {
408 mPowerMode = mode;
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700409}
410
Prashant Malani2c9b11f2014-05-25 01:36:31 -0700411int DisplayDevice::getPowerMode() const {
412 return mPowerMode;
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700413}
414
Prashant Malani2c9b11f2014-05-25 01:36:31 -0700415bool DisplayDevice::isDisplayOn() const {
416 return (mPowerMode != HWC_POWER_MODE_OFF);
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700417}
418
419// ----------------------------------------------------------------------------
Michael Lentine6c9e34a2014-07-14 13:48:55 -0700420void DisplayDevice::setActiveConfig(int mode) {
421 mActiveConfig = mode;
422}
423
424int DisplayDevice::getActiveConfig() const {
425 return mActiveConfig;
426}
427
428// ----------------------------------------------------------------------------
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000429#ifdef USE_HWC2
Michael Wright28f24d02016-07-12 13:30:53 -0700430void DisplayDevice::setActiveColorMode(android_color_mode_t mode) {
431 mActiveColorMode = mode;
432}
433
434android_color_mode_t DisplayDevice::getActiveColorMode() const {
435 return mActiveColorMode;
436}
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000437#endif
Michael Wright28f24d02016-07-12 13:30:53 -0700438
439// ----------------------------------------------------------------------------
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700440
Mathias Agopian28947d72012-08-08 18:51:15 -0700441void DisplayDevice::setLayerStack(uint32_t stack) {
442 mLayerStack = stack;
443 dirtyRegion.set(bounds());
444}
445
446// ----------------------------------------------------------------------------
447
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700448uint32_t DisplayDevice::getOrientationTransform() const {
449 uint32_t transform = 0;
450 switch (mOrientation) {
451 case DisplayState::eOrientationDefault:
452 transform = Transform::ROT_0;
453 break;
454 case DisplayState::eOrientation90:
455 transform = Transform::ROT_90;
456 break;
457 case DisplayState::eOrientation180:
458 transform = Transform::ROT_180;
459 break;
460 case DisplayState::eOrientation270:
461 transform = Transform::ROT_270;
462 break;
463 }
464 return transform;
465}
466
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700467status_t DisplayDevice::orientationToTransfrom(
Mathias Agopian1b031492012-06-20 17:51:20 -0700468 int orientation, int w, int h, Transform* tr)
469{
470 uint32_t flags = 0;
471 switch (orientation) {
Mathias Agopian3165cc22012-08-08 19:42:09 -0700472 case DisplayState::eOrientationDefault:
Mathias Agopian1b031492012-06-20 17:51:20 -0700473 flags = Transform::ROT_0;
474 break;
Mathias Agopian3165cc22012-08-08 19:42:09 -0700475 case DisplayState::eOrientation90:
Mathias Agopian1b031492012-06-20 17:51:20 -0700476 flags = Transform::ROT_90;
477 break;
Mathias Agopian3165cc22012-08-08 19:42:09 -0700478 case DisplayState::eOrientation180:
Mathias Agopian1b031492012-06-20 17:51:20 -0700479 flags = Transform::ROT_180;
480 break;
Mathias Agopian3165cc22012-08-08 19:42:09 -0700481 case DisplayState::eOrientation270:
Mathias Agopian1b031492012-06-20 17:51:20 -0700482 flags = Transform::ROT_270;
483 break;
484 default:
485 return BAD_VALUE;
486 }
487 tr->set(flags, w, h);
488 return NO_ERROR;
489}
490
Michael Lentine47e45402014-07-18 15:34:25 -0700491void DisplayDevice::setDisplaySize(const int newWidth, const int newHeight) {
492 dirtyRegion.set(getBounds());
493
Michael Lentinef2568de2014-08-20 10:51:23 -0700494 if (mSurface != EGL_NO_SURFACE) {
495 eglDestroySurface(mDisplay, mSurface);
496 mSurface = EGL_NO_SURFACE;
497 }
498
Michael Lentine47e45402014-07-18 15:34:25 -0700499 mDisplaySurface->resizeBuffers(newWidth, newHeight);
500
501 ANativeWindow* const window = mNativeWindow.get();
502 mSurface = eglCreateWindowSurface(mDisplay, mConfig, window, NULL);
503 eglQuerySurface(mDisplay, mSurface, EGL_WIDTH, &mDisplayWidth);
504 eglQuerySurface(mDisplay, mSurface, EGL_HEIGHT, &mDisplayHeight);
505
506 LOG_FATAL_IF(mDisplayWidth != newWidth,
507 "Unable to set new width to %d", newWidth);
508 LOG_FATAL_IF(mDisplayHeight != newHeight,
509 "Unable to set new height to %d", newHeight);
510}
511
Mathias Agopian00e8c7a2012-09-04 19:30:46 -0700512void DisplayDevice::setProjection(int orientation,
Mathias Agopianf5f714a2013-02-26 16:54:05 -0800513 const Rect& newViewport, const Rect& newFrame) {
514 Rect viewport(newViewport);
515 Rect frame(newFrame);
516
517 const int w = mDisplayWidth;
518 const int h = mDisplayHeight;
519
520 Transform R;
521 DisplayDevice::orientationToTransfrom(orientation, w, h, &R);
522
523 if (!frame.isValid()) {
524 // the destination frame can be invalid if it has never been set,
525 // in that case we assume the whole display frame.
526 frame = Rect(w, h);
527 }
528
529 if (viewport.isEmpty()) {
530 // viewport can be invalid if it has never been set, in that case
531 // we assume the whole display size.
532 // it's also invalid to have an empty viewport, so we handle that
533 // case in the same way.
534 viewport = Rect(w, h);
535 if (R.getOrientation() & Transform::ROT_90) {
536 // viewport is always specified in the logical orientation
537 // of the display (ie: post-rotation).
538 swap(viewport.right, viewport.bottom);
539 }
540 }
541
542 dirtyRegion.set(getBounds());
543
544 Transform TL, TP, S;
545 float src_width = viewport.width();
546 float src_height = viewport.height();
547 float dst_width = frame.width();
548 float dst_height = frame.height();
549 if (src_width != dst_width || src_height != dst_height) {
550 float sx = dst_width / src_width;
551 float sy = dst_height / src_height;
552 S.set(sx, 0, 0, sy);
553 }
554
555 float src_x = viewport.left;
556 float src_y = viewport.top;
557 float dst_x = frame.left;
558 float dst_y = frame.top;
559 TL.set(-src_x, -src_y);
560 TP.set(dst_x, dst_y);
561
562 // The viewport and frame are both in the logical orientation.
563 // Apply the logical translation, scale to physical size, apply the
564 // physical translation and finally rotate to the physical orientation.
565 mGlobalTransform = R * TP * S * TL;
566
567 const uint8_t type = mGlobalTransform.getType();
568 mNeedsFiltering = (!mGlobalTransform.preserveRects() ||
569 (type >= Transform::SCALE));
570
571 mScissor = mGlobalTransform.transform(viewport);
572 if (mScissor.isEmpty()) {
Mathias Agopian6c7f25a2013-05-09 20:37:10 -0700573 mScissor = getBounds();
Mathias Agopianf5f714a2013-02-26 16:54:05 -0800574 }
575
Mathias Agopianda8d0a52012-09-04 15:05:38 -0700576 mOrientation = orientation;
Pablo Ceballos021623b2016-04-15 17:31:51 -0700577 if (mType == DisplayType::DISPLAY_PRIMARY) {
578 uint32_t transform = 0;
579 switch (mOrientation) {
580 case DisplayState::eOrientationDefault:
581 transform = Transform::ROT_0;
582 break;
583 case DisplayState::eOrientation90:
584 transform = Transform::ROT_90;
585 break;
586 case DisplayState::eOrientation180:
587 transform = Transform::ROT_180;
588 break;
589 case DisplayState::eOrientation270:
590 transform = Transform::ROT_270;
591 break;
592 }
593 sPrimaryDisplayOrientation = transform;
594 }
Mathias Agopian00e8c7a2012-09-04 19:30:46 -0700595 mViewport = viewport;
596 mFrame = frame;
Mathias Agopian1b031492012-06-20 17:51:20 -0700597}
Mathias Agopian1d12d8a2012-09-18 01:38:00 -0700598
Pablo Ceballos021623b2016-04-15 17:31:51 -0700599uint32_t DisplayDevice::getPrimaryDisplayOrientationTransform() {
600 return sPrimaryDisplayOrientation;
601}
602
Mathias Agopian74d211a2013-04-22 16:55:35 +0200603void DisplayDevice::dump(String8& result) const {
Mathias Agopian1d12d8a2012-09-18 01:38:00 -0700604 const Transform& tr(mGlobalTransform);
Mathias Agopian74d211a2013-04-22 16:55:35 +0200605 result.appendFormat(
Mathias Agopian1d12d8a2012-09-18 01:38:00 -0700606 "+ DisplayDevice: %s\n"
Jesse Hall02d86562013-03-25 14:43:23 -0700607 " type=%x, hwcId=%d, layerStack=%u, (%4dx%4d), ANativeWindow=%p, orient=%2d (type=%08x), "
Pablo Ceballosb5b35632016-02-23 11:18:51 -0800608 "flips=%u, isSecure=%d, powerMode=%d, activeConfig=%d, numLayers=%zu\n"
Mathias Agopian766dc492012-10-30 18:08:06 -0700609 " v:[%d,%d,%d,%d], f:[%d,%d,%d,%d], s:[%d,%d,%d,%d],"
Mathias Agopian1d12d8a2012-09-18 01:38:00 -0700610 "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 -0700611 mDisplayName.string(), mType, mHwcDisplayId,
Mathias Agopian1d12d8a2012-09-18 01:38:00 -0700612 mLayerStack, mDisplayWidth, mDisplayHeight, mNativeWindow.get(),
613 mOrientation, tr.getType(), getPageFlipCount(),
Pablo Ceballosb5b35632016-02-23 11:18:51 -0800614 mIsSecure, mPowerMode, mActiveConfig,
Michael Lentine6c9e34a2014-07-14 13:48:55 -0700615 mVisibleLayersSortedByZ.size(),
Mathias Agopian1d12d8a2012-09-18 01:38:00 -0700616 mViewport.left, mViewport.top, mViewport.right, mViewport.bottom,
617 mFrame.left, mFrame.top, mFrame.right, mFrame.bottom,
Mathias Agopian766dc492012-10-30 18:08:06 -0700618 mScissor.left, mScissor.top, mScissor.right, mScissor.bottom,
Mathias Agopian1d12d8a2012-09-18 01:38:00 -0700619 tr[0][0], tr[1][0], tr[2][0],
620 tr[0][1], tr[1][1], tr[2][1],
621 tr[0][2], tr[1][2], tr[2][2]);
622
Jesse Hall99c7dbb2013-03-14 14:29:29 -0700623 String8 surfaceDump;
Dan Stozaf10c46e2014-11-11 10:32:31 -0800624 mDisplaySurface->dumpAsString(surfaceDump);
Jesse Hall99c7dbb2013-03-14 14:29:29 -0700625 result.append(surfaceDump);
Mathias Agopian1d12d8a2012-09-18 01:38:00 -0700626}
Irvelffc9efc2016-07-27 15:16:37 -0700627
628std::atomic<int32_t> DisplayDeviceState::nextDisplayId(1);
629
630DisplayDeviceState::DisplayDeviceState(DisplayDevice::DisplayType type, bool isSecure)
631 : type(type),
632 layerStack(DisplayDevice::NO_LAYER_STACK),
633 orientation(0),
634 width(0),
635 height(0),
636 isSecure(isSecure)
637{
638 viewport.makeInvalid();
639 frame.makeInvalid();
640}