blob: 5c2c0adf3a38d4636af0075121e65745ce7e425b [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"
Dan Stoza9e56aa02015-11-02 13:00:03 -080040#ifdef USE_HWC2
41#include "DisplayHardware/HWC2.h"
42#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
Mathias Agopiana4912602012-07-12 14:25:33 -070050// ----------------------------------------------------------------------------
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080051using namespace android;
Mathias Agopiana4912602012-07-12 14:25:33 -070052// ----------------------------------------------------------------------------
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080053
Andreas Gampe89fd4f72014-11-13 14:18:56 -080054#ifdef EGL_ANDROID_swap_rectangle
55static constexpr bool kEGLAndroidSwapRectangle = true;
56#else
57static constexpr bool kEGLAndroidSwapRectangle = false;
58#endif
59
60#if !defined(EGL_EGLEXT_PROTOTYPES) || !defined(EGL_ANDROID_swap_rectangle)
61// Dummy implementation in case it is missing.
62inline void eglSetSwapRectangleANDROID (EGLDisplay, EGLSurface, EGLint, EGLint, EGLint, EGLint) {
63}
64#endif
65
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080066/*
67 * Initialize the display to the specified values.
68 *
69 */
70
Pablo Ceballos021623b2016-04-15 17:31:51 -070071uint32_t DisplayDevice::sPrimaryDisplayOrientation = 0;
72
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -070073DisplayDevice::DisplayDevice(
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080074 const sp<SurfaceFlinger>& flinger,
Jamie Gennisdd3cb842012-10-19 18:19:11 -070075 DisplayType type,
Jesse Hallffe1f192013-03-22 15:13:48 -070076 int32_t hwcId,
Dan Stoza9e56aa02015-11-02 13:00:03 -080077#ifndef USE_HWC2
Jesse Hall19e87292013-12-23 21:02:15 -080078 int format,
Dan Stoza9e56aa02015-11-02 13:00:03 -080079#endif
Jamie Gennisdd3cb842012-10-19 18:19:11 -070080 bool isSecure,
81 const wp<IBinder>& displayToken,
Jesse Hall99c7dbb2013-03-14 14:29:29 -070082 const sp<DisplaySurface>& displaySurface,
Mathias Agopiandb89edc2013-08-02 01:40:18 -070083 const sp<IGraphicBufferProducer>& producer,
Mathias Agopiana4912602012-07-12 14:25:33 -070084 EGLConfig config)
Jesse Hallb7a05492014-08-14 15:45:06 -070085 : lastCompositionHadVisibleLayers(false),
86 mFlinger(flinger),
Dan Stoza9e56aa02015-11-02 13:00:03 -080087 mType(type),
88 mHwcDisplayId(hwcId),
Chih-Wei Huang27e25622013-01-07 17:33:56 +080089 mDisplayToken(displayToken),
Jesse Hall99c7dbb2013-03-14 14:29:29 -070090 mDisplaySurface(displaySurface),
Mathias Agopian92a979a2012-08-02 18:32:23 -070091 mDisplay(EGL_NO_DISPLAY),
92 mSurface(EGL_NO_SURFACE),
Dan Stoza9e56aa02015-11-02 13:00:03 -080093 mDisplayWidth(),
94 mDisplayHeight(),
95#ifndef USE_HWC2
96 mFormat(),
97#endif
Mathias Agopian92a979a2012-08-02 18:32:23 -070098 mFlags(),
99 mPageFlipCount(),
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700100 mIsSecure(isSecure),
Jesse Hall01e29052013-02-19 16:13:35 -0800101 mLayerStack(NO_LAYER_STACK),
Prashant Malani2c9b11f2014-05-25 01:36:31 -0700102 mOrientation(),
Michael Lentine6c9e34a2014-07-14 13:48:55 -0700103 mPowerMode(HWC_POWER_MODE_OFF),
104 mActiveConfig(0)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800105{
james.zhang5e8eb5e2015-12-01 17:55:11 +0800106 Surface* surface;
107 mNativeWindow = surface = new Surface(producer, false);
Jesse Hallffe1f192013-03-22 15:13:48 -0700108 ANativeWindow* const window = mNativeWindow.get();
109
Jesse Hallffe1f192013-03-22 15:13:48 -0700110 /*
111 * Create our display's surface
112 */
113
james.zhang5e8eb5e2015-12-01 17:55:11 +0800114 EGLSurface eglSurface;
Jesse Hallffe1f192013-03-22 15:13:48 -0700115 EGLDisplay display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
Jesse Hall19e87292013-12-23 21:02:15 -0800116 if (config == EGL_NO_CONFIG) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800117#ifdef USE_HWC2
118 config = RenderEngine::chooseEglConfig(display, PIXEL_FORMAT_RGBA_8888);
119#else
Jesse Hall19e87292013-12-23 21:02:15 -0800120 config = RenderEngine::chooseEglConfig(display, format);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800121#endif
Jesse Hall19e87292013-12-23 21:02:15 -0800122 }
james.zhang5e8eb5e2015-12-01 17:55:11 +0800123 eglSurface = eglCreateWindowSurface(display, config, window, NULL);
124 eglQuerySurface(display, eglSurface, EGL_WIDTH, &mDisplayWidth);
125 eglQuerySurface(display, eglSurface, EGL_HEIGHT, &mDisplayHeight);
Jesse Hallffe1f192013-03-22 15:13:48 -0700126
John Dong4ee56962014-02-21 12:37:59 -0800127 // Make sure that composition can never be stalled by a virtual display
128 // consumer that isn't processing buffers fast enough. We have to do this
129 // in two places:
130 // * Here, in case the display is composed entirely by HWC.
131 // * In makeCurrent(), using eglSwapInterval. Some EGL drivers set the
132 // window's swap interval in eglMakeCurrent, so they'll override the
133 // interval we set here.
134 if (mType >= DisplayDevice::DISPLAY_VIRTUAL)
135 window->setSwapInterval(window, 0);
136
Michael Lentine47e45402014-07-18 15:34:25 -0700137 mConfig = config;
Jesse Hallffe1f192013-03-22 15:13:48 -0700138 mDisplay = display;
james.zhang5e8eb5e2015-12-01 17:55:11 +0800139 mSurface = eglSurface;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800140#ifndef USE_HWC2
141 mFormat = format;
142#endif
Jesse Hallffe1f192013-03-22 15:13:48 -0700143 mPageFlipCount = 0;
144 mViewport.makeInvalid();
145 mFrame.makeInvalid();
146
147 // virtual displays are always considered enabled
Prashant Malani2c9b11f2014-05-25 01:36:31 -0700148 mPowerMode = (mType >= DisplayDevice::DISPLAY_VIRTUAL) ?
149 HWC_POWER_MODE_NORMAL : HWC_POWER_MODE_OFF;
Jesse Hallffe1f192013-03-22 15:13:48 -0700150
151 // Name the display. The name will be replaced shortly if the display
152 // was created with createDisplay().
153 switch (mType) {
154 case DISPLAY_PRIMARY:
155 mDisplayName = "Built-in Screen";
156 break;
157 case DISPLAY_EXTERNAL:
158 mDisplayName = "HDMI Screen";
159 break;
160 default:
161 mDisplayName = "Virtual Screen"; // e.g. Overlay #n
162 break;
163 }
164
165 // initialize the display orientation transform.
166 setProjection(DisplayState::eOrientationDefault, mViewport, mFrame);
james.zhang5e8eb5e2015-12-01 17:55:11 +0800167
168#ifdef NUM_FRAMEBUFFER_SURFACE_BUFFERS
169 surface->allocateBuffers();
170#endif
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800171}
172
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700173DisplayDevice::~DisplayDevice() {
Mathias Agopian92a979a2012-08-02 18:32:23 -0700174 if (mSurface != EGL_NO_SURFACE) {
175 eglDestroySurface(mDisplay, mSurface);
176 mSurface = EGL_NO_SURFACE;
177 }
178}
179
Jesse Hall02d86562013-03-25 14:43:23 -0700180void DisplayDevice::disconnect(HWComposer& hwc) {
181 if (mHwcDisplayId >= 0) {
182 hwc.disconnectDisplay(mHwcDisplayId);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800183#ifndef USE_HWC2
Jesse Hall02d86562013-03-25 14:43:23 -0700184 if (mHwcDisplayId >= DISPLAY_VIRTUAL)
185 hwc.freeDisplayId(mHwcDisplayId);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800186#endif
Jesse Hall02d86562013-03-25 14:43:23 -0700187 mHwcDisplayId = -1;
188 }
189}
190
Mathias Agopian92a979a2012-08-02 18:32:23 -0700191bool DisplayDevice::isValid() const {
192 return mFlinger != NULL;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800193}
194
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700195int DisplayDevice::getWidth() const {
Mathias Agopiana4912602012-07-12 14:25:33 -0700196 return mDisplayWidth;
197}
198
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700199int DisplayDevice::getHeight() const {
Mathias Agopiana4912602012-07-12 14:25:33 -0700200 return mDisplayHeight;
201}
202
Dan Stoza9e56aa02015-11-02 13:00:03 -0800203#ifndef USE_HWC2
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700204PixelFormat DisplayDevice::getFormat() const {
Mathias Agopiana4912602012-07-12 14:25:33 -0700205 return mFormat;
206}
Dan Stoza9e56aa02015-11-02 13:00:03 -0800207#endif
Mathias Agopiana4912602012-07-12 14:25:33 -0700208
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700209EGLSurface DisplayDevice::getEGLSurface() const {
Mathias Agopiana4912602012-07-12 14:25:33 -0700210 return mSurface;
211}
212
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700213void DisplayDevice::setDisplayName(const String8& displayName) {
214 if (!displayName.isEmpty()) {
215 // never override the name with an empty name
216 mDisplayName = displayName;
217 }
218}
219
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700220uint32_t DisplayDevice::getPageFlipCount() const {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700221 return mPageFlipCount;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800222}
223
Dan Stoza9e56aa02015-11-02 13:00:03 -0800224#ifndef USE_HWC2
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700225status_t DisplayDevice::compositionComplete() const {
Jesse Hall99c7dbb2013-03-14 14:29:29 -0700226 return mDisplaySurface->compositionComplete();
Mathias Agopian74faca22009-09-17 16:18:16 -0700227}
Dan Stoza9e56aa02015-11-02 13:00:03 -0800228#endif
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800229
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700230void DisplayDevice::flip(const Region& dirty) const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800231{
Mathias Agopian875d8e12013-06-07 15:35:48 -0700232 mFlinger->getRenderEngine().checkErrors();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800233
Andreas Gampe89fd4f72014-11-13 14:18:56 -0800234 if (kEGLAndroidSwapRectangle) {
235 if (mFlags & SWAP_RECTANGLE) {
236 const Region newDirty(dirty.intersect(bounds()));
237 const Rect b(newDirty.getBounds());
238 eglSetSwapRectangleANDROID(mDisplay, mSurface,
239 b.left, b.top, b.width(), b.height());
240 }
Jesse Hall01e29052013-02-19 16:13:35 -0800241 }
Mathias Agopiand8707032012-09-18 01:21:55 -0700242
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700243 mPageFlipCount++;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800244}
245
Dan Stoza71433162014-02-04 16:22:36 -0800246status_t DisplayDevice::beginFrame(bool mustRecompose) const {
247 return mDisplaySurface->beginFrame(mustRecompose);
Jesse Hall028dc8f2013-08-20 16:35:32 -0700248}
249
Dan Stoza9e56aa02015-11-02 13:00:03 -0800250#ifdef USE_HWC2
251status_t DisplayDevice::prepareFrame(HWComposer& hwc) {
252 status_t error = hwc.prepare(*this);
253 if (error != NO_ERROR) {
254 return error;
255 }
256
257 DisplaySurface::CompositionType compositionType;
258 bool hasClient = hwc.hasClientComposition(mHwcDisplayId);
259 bool hasDevice = hwc.hasDeviceComposition(mHwcDisplayId);
260 if (hasClient && hasDevice) {
261 compositionType = DisplaySurface::COMPOSITION_MIXED;
262 } else if (hasClient) {
263 compositionType = DisplaySurface::COMPOSITION_GLES;
264 } else if (hasDevice) {
265 compositionType = DisplaySurface::COMPOSITION_HWC;
266 } else {
267 // Nothing to do -- when turning the screen off we get a frame like
268 // this. Call it a HWC frame since we won't be doing any GLES work but
269 // will do a prepare/set cycle.
270 compositionType = DisplaySurface::COMPOSITION_HWC;
271 }
272 return mDisplaySurface->prepareFrame(compositionType);
273}
274#else
Jesse Hall38efe862013-04-06 23:12:29 -0700275status_t DisplayDevice::prepareFrame(const HWComposer& hwc) const {
276 DisplaySurface::CompositionType compositionType;
277 bool haveGles = hwc.hasGlesComposition(mHwcDisplayId);
278 bool haveHwc = hwc.hasHwcComposition(mHwcDisplayId);
279 if (haveGles && haveHwc) {
280 compositionType = DisplaySurface::COMPOSITION_MIXED;
281 } else if (haveGles) {
282 compositionType = DisplaySurface::COMPOSITION_GLES;
283 } else if (haveHwc) {
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}
Dan Stoza9e56aa02015-11-02 13:00:03 -0800293#endif
Jesse Hall38efe862013-04-06 23:12:29 -0700294
Mathias Agopianda27af92012-09-13 18:17:13 -0700295void DisplayDevice::swapBuffers(HWComposer& hwc) const {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800296#ifdef USE_HWC2
297 if (hwc.hasClientComposition(mHwcDisplayId)) {
298#else
Andy McFadden22a99f02013-08-20 16:04:47 -0700299 // We need to call eglSwapBuffers() if:
300 // (1) we don't have a hardware composer, or
301 // (2) we did GLES composition this frame, and either
302 // (a) we have framebuffer target support (not present on legacy
303 // devices, where HWComposer::commit() handles things); or
304 // (b) this is a virtual display
Jesse Hall99c7dbb2013-03-14 14:29:29 -0700305 if (hwc.initCheck() != NO_ERROR ||
306 (hwc.hasGlesComposition(mHwcDisplayId) &&
Andy McFadden22a99f02013-08-20 16:04:47 -0700307 (hwc.supportsFramebufferTarget() || mType >= DISPLAY_VIRTUAL))) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800308#endif
Jesse Hall99c7dbb2013-03-14 14:29:29 -0700309 EGLBoolean success = eglSwapBuffers(mDisplay, mSurface);
310 if (!success) {
311 EGLint error = eglGetError();
312 if (error == EGL_CONTEXT_LOST ||
313 mType == DisplayDevice::DISPLAY_PRIMARY) {
314 LOG_ALWAYS_FATAL("eglSwapBuffers(%p, %p) failed with 0x%08x",
315 mDisplay, mSurface, error);
316 } else {
317 ALOGE("eglSwapBuffers(%p, %p) failed with 0x%08x",
318 mDisplay, mSurface, error);
Mathias Agopianda27af92012-09-13 18:17:13 -0700319 }
320 }
321 }
Mathias Agopian52e21482012-09-24 18:07:21 -0700322
Jesse Hall99c7dbb2013-03-14 14:29:29 -0700323 status_t result = mDisplaySurface->advanceFrame();
324 if (result != NO_ERROR) {
325 ALOGE("[%s] failed pushing new frame to HWC: %d",
326 mDisplayName.string(), result);
Mathias Agopian32341382012-09-25 19:16:28 -0700327 }
Mathias Agopianda27af92012-09-13 18:17:13 -0700328}
329
Dan Stoza9e56aa02015-11-02 13:00:03 -0800330#ifdef USE_HWC2
331void DisplayDevice::onSwapBuffersCompleted() const {
332 mDisplaySurface->onFrameCommitted();
333}
334#else
Mathias Agopianda27af92012-09-13 18:17:13 -0700335void DisplayDevice::onSwapBuffersCompleted(HWComposer& hwc) const {
336 if (hwc.initCheck() == NO_ERROR) {
Jesse Hall851cfe82013-03-20 13:44:00 -0700337 mDisplaySurface->onFrameCommitted();
Mathias Agopianda27af92012-09-13 18:17:13 -0700338 }
339}
Dan Stoza9e56aa02015-11-02 13:00:03 -0800340#endif
Mathias Agopianda27af92012-09-13 18:17:13 -0700341
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700342uint32_t DisplayDevice::getFlags() const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800343{
344 return mFlags;
345}
346
Mathias Agopian875d8e12013-06-07 15:35:48 -0700347EGLBoolean DisplayDevice::makeCurrent(EGLDisplay dpy, EGLContext ctx) const {
Mathias Agopianda8d0a52012-09-04 15:05:38 -0700348 EGLBoolean result = EGL_TRUE;
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700349 EGLSurface sur = eglGetCurrentSurface(EGL_DRAW);
Mathias Agopian875d8e12013-06-07 15:35:48 -0700350 if (sur != mSurface) {
351 result = eglMakeCurrent(dpy, mSurface, mSurface, ctx);
Mathias Agopianda8d0a52012-09-04 15:05:38 -0700352 if (result == EGL_TRUE) {
Jesse Hallf460f552013-08-06 17:08:53 -0700353 if (mType >= DisplayDevice::DISPLAY_VIRTUAL)
354 eglSwapInterval(dpy, 0);
Mathias Agopianda8d0a52012-09-04 15:05:38 -0700355 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700356 }
Mathias Agopian931bda12013-08-28 18:11:46 -0700357 setViewportAndProjection();
Mathias Agopianda8d0a52012-09-04 15:05:38 -0700358 return result;
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700359}
360
Mathias Agopian875d8e12013-06-07 15:35:48 -0700361void DisplayDevice::setViewportAndProjection() const {
362 size_t w = mDisplayWidth;
363 size_t h = mDisplayHeight;
Dan Stozac1879002014-05-22 15:59:05 -0700364 Rect sourceCrop(0, 0, w, h);
Riley Andrewsc3ebe662014-09-04 16:20:31 -0700365 mFlinger->getRenderEngine().setViewportAndProjection(w, h, sourceCrop, h,
366 false, Transform::ROT_0);
Mathias Agopianbae92d02012-09-28 01:00:47 -0700367}
368
Dan Stoza9e56aa02015-11-02 13:00:03 -0800369const sp<Fence>& DisplayDevice::getClientTargetAcquireFence() const {
370 return mDisplaySurface->getClientTargetAcquireFence();
371}
Dan Stoza9e56aa02015-11-02 13:00:03 -0800372
Mathias Agopian1b031492012-06-20 17:51:20 -0700373// ----------------------------------------------------------------------------
374
Mathias Agopian13127d82013-03-05 17:47:11 -0800375void DisplayDevice::setVisibleLayersSortedByZ(const Vector< sp<Layer> >& layers) {
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700376 mVisibleLayersSortedByZ = layers;
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700377}
378
Mathias Agopian13127d82013-03-05 17:47:11 -0800379const Vector< sp<Layer> >& DisplayDevice::getVisibleLayersSortedByZ() const {
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700380 return mVisibleLayersSortedByZ;
381}
382
Mathias Agopiancd60f992012-08-16 16:28:27 -0700383Region DisplayDevice::getDirtyRegion(bool repaintEverything) const {
384 Region dirty;
Mathias Agopiancd60f992012-08-16 16:28:27 -0700385 if (repaintEverything) {
386 dirty.set(getBounds());
387 } else {
Mathias Agopianda8d0a52012-09-04 15:05:38 -0700388 const Transform& planeTransform(mGlobalTransform);
Mathias Agopiancd60f992012-08-16 16:28:27 -0700389 dirty = planeTransform.transform(this->dirtyRegion);
390 dirty.andSelf(getBounds());
391 }
392 return dirty;
393}
394
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700395// ----------------------------------------------------------------------------
Prashant Malani2c9b11f2014-05-25 01:36:31 -0700396void DisplayDevice::setPowerMode(int mode) {
397 mPowerMode = mode;
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700398}
399
Prashant Malani2c9b11f2014-05-25 01:36:31 -0700400int DisplayDevice::getPowerMode() const {
401 return mPowerMode;
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700402}
403
Prashant Malani2c9b11f2014-05-25 01:36:31 -0700404bool DisplayDevice::isDisplayOn() const {
405 return (mPowerMode != HWC_POWER_MODE_OFF);
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700406}
407
408// ----------------------------------------------------------------------------
Michael Lentine6c9e34a2014-07-14 13:48:55 -0700409void DisplayDevice::setActiveConfig(int mode) {
410 mActiveConfig = mode;
411}
412
413int DisplayDevice::getActiveConfig() const {
414 return mActiveConfig;
415}
416
417// ----------------------------------------------------------------------------
Michael Wright28f24d02016-07-12 13:30:53 -0700418#ifdef USE_HWC2
419void DisplayDevice::setActiveColorMode(android_color_mode_t mode) {
420 mActiveColorMode = mode;
421}
422
423android_color_mode_t DisplayDevice::getActiveColorMode() const {
424 return mActiveColorMode;
425}
426#endif
427
428// ----------------------------------------------------------------------------
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700429
Mathias Agopian28947d72012-08-08 18:51:15 -0700430void DisplayDevice::setLayerStack(uint32_t stack) {
431 mLayerStack = stack;
432 dirtyRegion.set(bounds());
433}
434
435// ----------------------------------------------------------------------------
436
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700437uint32_t DisplayDevice::getOrientationTransform() const {
438 uint32_t transform = 0;
439 switch (mOrientation) {
440 case DisplayState::eOrientationDefault:
441 transform = Transform::ROT_0;
442 break;
443 case DisplayState::eOrientation90:
444 transform = Transform::ROT_90;
445 break;
446 case DisplayState::eOrientation180:
447 transform = Transform::ROT_180;
448 break;
449 case DisplayState::eOrientation270:
450 transform = Transform::ROT_270;
451 break;
452 }
453 return transform;
454}
455
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700456status_t DisplayDevice::orientationToTransfrom(
Mathias Agopian1b031492012-06-20 17:51:20 -0700457 int orientation, int w, int h, Transform* tr)
458{
459 uint32_t flags = 0;
460 switch (orientation) {
Mathias Agopian3165cc22012-08-08 19:42:09 -0700461 case DisplayState::eOrientationDefault:
Mathias Agopian1b031492012-06-20 17:51:20 -0700462 flags = Transform::ROT_0;
463 break;
Mathias Agopian3165cc22012-08-08 19:42:09 -0700464 case DisplayState::eOrientation90:
Mathias Agopian1b031492012-06-20 17:51:20 -0700465 flags = Transform::ROT_90;
466 break;
Mathias Agopian3165cc22012-08-08 19:42:09 -0700467 case DisplayState::eOrientation180:
Mathias Agopian1b031492012-06-20 17:51:20 -0700468 flags = Transform::ROT_180;
469 break;
Mathias Agopian3165cc22012-08-08 19:42:09 -0700470 case DisplayState::eOrientation270:
Mathias Agopian1b031492012-06-20 17:51:20 -0700471 flags = Transform::ROT_270;
472 break;
473 default:
474 return BAD_VALUE;
475 }
476 tr->set(flags, w, h);
477 return NO_ERROR;
478}
479
Michael Lentine47e45402014-07-18 15:34:25 -0700480void DisplayDevice::setDisplaySize(const int newWidth, const int newHeight) {
481 dirtyRegion.set(getBounds());
482
Michael Lentinef2568de2014-08-20 10:51:23 -0700483 if (mSurface != EGL_NO_SURFACE) {
484 eglDestroySurface(mDisplay, mSurface);
485 mSurface = EGL_NO_SURFACE;
486 }
487
Michael Lentine47e45402014-07-18 15:34:25 -0700488 mDisplaySurface->resizeBuffers(newWidth, newHeight);
489
490 ANativeWindow* const window = mNativeWindow.get();
491 mSurface = eglCreateWindowSurface(mDisplay, mConfig, window, NULL);
492 eglQuerySurface(mDisplay, mSurface, EGL_WIDTH, &mDisplayWidth);
493 eglQuerySurface(mDisplay, mSurface, EGL_HEIGHT, &mDisplayHeight);
494
495 LOG_FATAL_IF(mDisplayWidth != newWidth,
496 "Unable to set new width to %d", newWidth);
497 LOG_FATAL_IF(mDisplayHeight != newHeight,
498 "Unable to set new height to %d", newHeight);
499}
500
Mathias Agopian00e8c7a2012-09-04 19:30:46 -0700501void DisplayDevice::setProjection(int orientation,
Mathias Agopianf5f714a2013-02-26 16:54:05 -0800502 const Rect& newViewport, const Rect& newFrame) {
503 Rect viewport(newViewport);
504 Rect frame(newFrame);
505
506 const int w = mDisplayWidth;
507 const int h = mDisplayHeight;
508
509 Transform R;
510 DisplayDevice::orientationToTransfrom(orientation, w, h, &R);
511
512 if (!frame.isValid()) {
513 // the destination frame can be invalid if it has never been set,
514 // in that case we assume the whole display frame.
515 frame = Rect(w, h);
516 }
517
518 if (viewport.isEmpty()) {
519 // viewport can be invalid if it has never been set, in that case
520 // we assume the whole display size.
521 // it's also invalid to have an empty viewport, so we handle that
522 // case in the same way.
523 viewport = Rect(w, h);
524 if (R.getOrientation() & Transform::ROT_90) {
525 // viewport is always specified in the logical orientation
526 // of the display (ie: post-rotation).
527 swap(viewport.right, viewport.bottom);
528 }
529 }
530
531 dirtyRegion.set(getBounds());
532
533 Transform TL, TP, S;
534 float src_width = viewport.width();
535 float src_height = viewport.height();
536 float dst_width = frame.width();
537 float dst_height = frame.height();
538 if (src_width != dst_width || src_height != dst_height) {
539 float sx = dst_width / src_width;
540 float sy = dst_height / src_height;
541 S.set(sx, 0, 0, sy);
542 }
543
544 float src_x = viewport.left;
545 float src_y = viewport.top;
546 float dst_x = frame.left;
547 float dst_y = frame.top;
548 TL.set(-src_x, -src_y);
549 TP.set(dst_x, dst_y);
550
551 // The viewport and frame are both in the logical orientation.
552 // Apply the logical translation, scale to physical size, apply the
553 // physical translation and finally rotate to the physical orientation.
554 mGlobalTransform = R * TP * S * TL;
555
556 const uint8_t type = mGlobalTransform.getType();
557 mNeedsFiltering = (!mGlobalTransform.preserveRects() ||
558 (type >= Transform::SCALE));
559
560 mScissor = mGlobalTransform.transform(viewport);
561 if (mScissor.isEmpty()) {
Mathias Agopian6c7f25a2013-05-09 20:37:10 -0700562 mScissor = getBounds();
Mathias Agopianf5f714a2013-02-26 16:54:05 -0800563 }
564
Mathias Agopianda8d0a52012-09-04 15:05:38 -0700565 mOrientation = orientation;
Pablo Ceballos021623b2016-04-15 17:31:51 -0700566 if (mType == DisplayType::DISPLAY_PRIMARY) {
567 uint32_t transform = 0;
568 switch (mOrientation) {
569 case DisplayState::eOrientationDefault:
570 transform = Transform::ROT_0;
571 break;
572 case DisplayState::eOrientation90:
573 transform = Transform::ROT_90;
574 break;
575 case DisplayState::eOrientation180:
576 transform = Transform::ROT_180;
577 break;
578 case DisplayState::eOrientation270:
579 transform = Transform::ROT_270;
580 break;
581 }
582 sPrimaryDisplayOrientation = transform;
583 }
Mathias Agopian00e8c7a2012-09-04 19:30:46 -0700584 mViewport = viewport;
585 mFrame = frame;
Mathias Agopian1b031492012-06-20 17:51:20 -0700586}
Mathias Agopian1d12d8a2012-09-18 01:38:00 -0700587
Pablo Ceballos021623b2016-04-15 17:31:51 -0700588uint32_t DisplayDevice::getPrimaryDisplayOrientationTransform() {
589 return sPrimaryDisplayOrientation;
590}
591
Mathias Agopian74d211a2013-04-22 16:55:35 +0200592void DisplayDevice::dump(String8& result) const {
Mathias Agopian1d12d8a2012-09-18 01:38:00 -0700593 const Transform& tr(mGlobalTransform);
Mathias Agopian74d211a2013-04-22 16:55:35 +0200594 result.appendFormat(
Mathias Agopian1d12d8a2012-09-18 01:38:00 -0700595 "+ DisplayDevice: %s\n"
Jesse Hall02d86562013-03-25 14:43:23 -0700596 " type=%x, hwcId=%d, layerStack=%u, (%4dx%4d), ANativeWindow=%p, orient=%2d (type=%08x), "
Pablo Ceballosb5b35632016-02-23 11:18:51 -0800597 "flips=%u, isSecure=%d, powerMode=%d, activeConfig=%d, numLayers=%zu\n"
Mathias Agopian766dc492012-10-30 18:08:06 -0700598 " v:[%d,%d,%d,%d], f:[%d,%d,%d,%d], s:[%d,%d,%d,%d],"
Mathias Agopian1d12d8a2012-09-18 01:38:00 -0700599 "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 -0700600 mDisplayName.string(), mType, mHwcDisplayId,
Mathias Agopian1d12d8a2012-09-18 01:38:00 -0700601 mLayerStack, mDisplayWidth, mDisplayHeight, mNativeWindow.get(),
602 mOrientation, tr.getType(), getPageFlipCount(),
Pablo Ceballosb5b35632016-02-23 11:18:51 -0800603 mIsSecure, mPowerMode, mActiveConfig,
Michael Lentine6c9e34a2014-07-14 13:48:55 -0700604 mVisibleLayersSortedByZ.size(),
Mathias Agopian1d12d8a2012-09-18 01:38:00 -0700605 mViewport.left, mViewport.top, mViewport.right, mViewport.bottom,
606 mFrame.left, mFrame.top, mFrame.right, mFrame.bottom,
Mathias Agopian766dc492012-10-30 18:08:06 -0700607 mScissor.left, mScissor.top, mScissor.right, mScissor.bottom,
Mathias Agopian1d12d8a2012-09-18 01:38:00 -0700608 tr[0][0], tr[1][0], tr[2][0],
609 tr[0][1], tr[1][1], tr[2][1],
610 tr[0][2], tr[1][2], tr[2][2]);
611
Jesse Hall99c7dbb2013-03-14 14:29:29 -0700612 String8 surfaceDump;
Dan Stozaf10c46e2014-11-11 10:32:31 -0800613 mDisplaySurface->dumpAsString(surfaceDump);
Jesse Hall99c7dbb2013-03-14 14:29:29 -0700614 result.append(surfaceDump);
Mathias Agopian1d12d8a2012-09-18 01:38:00 -0700615}