blob: 8d8e40d0dae4fdfb52b7d2f54e0068d6dd87d73e [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
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -070071DisplayDevice::DisplayDevice(
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080072 const sp<SurfaceFlinger>& flinger,
Jamie Gennisdd3cb842012-10-19 18:19:11 -070073 DisplayType type,
Jesse Hallffe1f192013-03-22 15:13:48 -070074 int32_t hwcId,
Dan Stoza9e56aa02015-11-02 13:00:03 -080075#ifndef USE_HWC2
Jesse Hall19e87292013-12-23 21:02:15 -080076 int format,
Dan Stoza9e56aa02015-11-02 13:00:03 -080077#endif
Jamie Gennisdd3cb842012-10-19 18:19:11 -070078 bool isSecure,
79 const wp<IBinder>& displayToken,
Jesse Hall99c7dbb2013-03-14 14:29:29 -070080 const sp<DisplaySurface>& displaySurface,
Mathias Agopiandb89edc2013-08-02 01:40:18 -070081 const sp<IGraphicBufferProducer>& producer,
Mathias Agopiana4912602012-07-12 14:25:33 -070082 EGLConfig config)
Jesse Hallb7a05492014-08-14 15:45:06 -070083 : lastCompositionHadVisibleLayers(false),
84 mFlinger(flinger),
Dan Stoza9e56aa02015-11-02 13:00:03 -080085 mType(type),
86 mHwcDisplayId(hwcId),
Chih-Wei Huang27e25622013-01-07 17:33:56 +080087 mDisplayToken(displayToken),
Jesse Hall99c7dbb2013-03-14 14:29:29 -070088 mDisplaySurface(displaySurface),
Mathias Agopian92a979a2012-08-02 18:32:23 -070089 mDisplay(EGL_NO_DISPLAY),
90 mSurface(EGL_NO_SURFACE),
Dan Stoza9e56aa02015-11-02 13:00:03 -080091 mDisplayWidth(),
92 mDisplayHeight(),
93#ifndef USE_HWC2
94 mFormat(),
95#endif
Mathias Agopian92a979a2012-08-02 18:32:23 -070096 mFlags(),
97 mPageFlipCount(),
Jamie Gennisdd3cb842012-10-19 18:19:11 -070098 mIsSecure(isSecure),
Jesse Hall01e29052013-02-19 16:13:35 -080099 mLayerStack(NO_LAYER_STACK),
Prashant Malani2c9b11f2014-05-25 01:36:31 -0700100 mOrientation(),
Michael Lentine6c9e34a2014-07-14 13:48:55 -0700101 mPowerMode(HWC_POWER_MODE_OFF),
102 mActiveConfig(0)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800103{
james.zhang5e8eb5e2015-12-01 17:55:11 +0800104 Surface* surface;
105 mNativeWindow = surface = new Surface(producer, false);
Jesse Hallffe1f192013-03-22 15:13:48 -0700106 ANativeWindow* const window = mNativeWindow.get();
107
Jesse Hallffe1f192013-03-22 15:13:48 -0700108 /*
109 * Create our display's surface
110 */
111
james.zhang5e8eb5e2015-12-01 17:55:11 +0800112 EGLSurface eglSurface;
Jesse Hallffe1f192013-03-22 15:13:48 -0700113 EGLDisplay display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
Jesse Hall19e87292013-12-23 21:02:15 -0800114 if (config == EGL_NO_CONFIG) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800115#ifdef USE_HWC2
116 config = RenderEngine::chooseEglConfig(display, PIXEL_FORMAT_RGBA_8888);
117#else
Jesse Hall19e87292013-12-23 21:02:15 -0800118 config = RenderEngine::chooseEglConfig(display, format);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800119#endif
Jesse Hall19e87292013-12-23 21:02:15 -0800120 }
james.zhang5e8eb5e2015-12-01 17:55:11 +0800121 eglSurface = eglCreateWindowSurface(display, config, window, NULL);
122 eglQuerySurface(display, eglSurface, EGL_WIDTH, &mDisplayWidth);
123 eglQuerySurface(display, eglSurface, EGL_HEIGHT, &mDisplayHeight);
Jesse Hallffe1f192013-03-22 15:13:48 -0700124
John Dong4ee56962014-02-21 12:37:59 -0800125 // Make sure that composition can never be stalled by a virtual display
126 // consumer that isn't processing buffers fast enough. We have to do this
127 // in two places:
128 // * Here, in case the display is composed entirely by HWC.
129 // * In makeCurrent(), using eglSwapInterval. Some EGL drivers set the
130 // window's swap interval in eglMakeCurrent, so they'll override the
131 // interval we set here.
132 if (mType >= DisplayDevice::DISPLAY_VIRTUAL)
133 window->setSwapInterval(window, 0);
134
Michael Lentine47e45402014-07-18 15:34:25 -0700135 mConfig = config;
Jesse Hallffe1f192013-03-22 15:13:48 -0700136 mDisplay = display;
james.zhang5e8eb5e2015-12-01 17:55:11 +0800137 mSurface = eglSurface;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800138#ifndef USE_HWC2
139 mFormat = format;
140#endif
Jesse Hallffe1f192013-03-22 15:13:48 -0700141 mPageFlipCount = 0;
142 mViewport.makeInvalid();
143 mFrame.makeInvalid();
144
145 // virtual displays are always considered enabled
Prashant Malani2c9b11f2014-05-25 01:36:31 -0700146 mPowerMode = (mType >= DisplayDevice::DISPLAY_VIRTUAL) ?
147 HWC_POWER_MODE_NORMAL : HWC_POWER_MODE_OFF;
Jesse Hallffe1f192013-03-22 15:13:48 -0700148
149 // Name the display. The name will be replaced shortly if the display
150 // was created with createDisplay().
151 switch (mType) {
152 case DISPLAY_PRIMARY:
153 mDisplayName = "Built-in Screen";
154 break;
155 case DISPLAY_EXTERNAL:
156 mDisplayName = "HDMI Screen";
157 break;
158 default:
159 mDisplayName = "Virtual Screen"; // e.g. Overlay #n
160 break;
161 }
162
163 // initialize the display orientation transform.
164 setProjection(DisplayState::eOrientationDefault, mViewport, mFrame);
james.zhang5e8eb5e2015-12-01 17:55:11 +0800165
166#ifdef NUM_FRAMEBUFFER_SURFACE_BUFFERS
167 surface->allocateBuffers();
168#endif
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800169}
170
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700171DisplayDevice::~DisplayDevice() {
Mathias Agopian92a979a2012-08-02 18:32:23 -0700172 if (mSurface != EGL_NO_SURFACE) {
173 eglDestroySurface(mDisplay, mSurface);
174 mSurface = EGL_NO_SURFACE;
175 }
176}
177
Jesse Hall02d86562013-03-25 14:43:23 -0700178void DisplayDevice::disconnect(HWComposer& hwc) {
179 if (mHwcDisplayId >= 0) {
180 hwc.disconnectDisplay(mHwcDisplayId);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800181#ifndef USE_HWC2
Jesse Hall02d86562013-03-25 14:43:23 -0700182 if (mHwcDisplayId >= DISPLAY_VIRTUAL)
183 hwc.freeDisplayId(mHwcDisplayId);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800184#endif
Jesse Hall02d86562013-03-25 14:43:23 -0700185 mHwcDisplayId = -1;
186 }
187}
188
Mathias Agopian92a979a2012-08-02 18:32:23 -0700189bool DisplayDevice::isValid() const {
190 return mFlinger != NULL;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800191}
192
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700193int DisplayDevice::getWidth() const {
Mathias Agopiana4912602012-07-12 14:25:33 -0700194 return mDisplayWidth;
195}
196
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700197int DisplayDevice::getHeight() const {
Mathias Agopiana4912602012-07-12 14:25:33 -0700198 return mDisplayHeight;
199}
200
Dan Stoza9e56aa02015-11-02 13:00:03 -0800201#ifndef USE_HWC2
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700202PixelFormat DisplayDevice::getFormat() const {
Mathias Agopiana4912602012-07-12 14:25:33 -0700203 return mFormat;
204}
Dan Stoza9e56aa02015-11-02 13:00:03 -0800205#endif
Mathias Agopiana4912602012-07-12 14:25:33 -0700206
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700207EGLSurface DisplayDevice::getEGLSurface() const {
Mathias Agopiana4912602012-07-12 14:25:33 -0700208 return mSurface;
209}
210
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700211void DisplayDevice::setDisplayName(const String8& displayName) {
212 if (!displayName.isEmpty()) {
213 // never override the name with an empty name
214 mDisplayName = displayName;
215 }
216}
217
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700218uint32_t DisplayDevice::getPageFlipCount() const {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700219 return mPageFlipCount;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800220}
221
Dan Stoza9e56aa02015-11-02 13:00:03 -0800222#ifndef USE_HWC2
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700223status_t DisplayDevice::compositionComplete() const {
Jesse Hall99c7dbb2013-03-14 14:29:29 -0700224 return mDisplaySurface->compositionComplete();
Mathias Agopian74faca22009-09-17 16:18:16 -0700225}
Dan Stoza9e56aa02015-11-02 13:00:03 -0800226#endif
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800227
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700228void DisplayDevice::flip(const Region& dirty) const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800229{
Mathias Agopian875d8e12013-06-07 15:35:48 -0700230 mFlinger->getRenderEngine().checkErrors();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800231
Andreas Gampe89fd4f72014-11-13 14:18:56 -0800232 if (kEGLAndroidSwapRectangle) {
233 if (mFlags & SWAP_RECTANGLE) {
234 const Region newDirty(dirty.intersect(bounds()));
235 const Rect b(newDirty.getBounds());
236 eglSetSwapRectangleANDROID(mDisplay, mSurface,
237 b.left, b.top, b.width(), b.height());
238 }
Jesse Hall01e29052013-02-19 16:13:35 -0800239 }
Mathias Agopiand8707032012-09-18 01:21:55 -0700240
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700241 mPageFlipCount++;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800242}
243
Dan Stoza71433162014-02-04 16:22:36 -0800244status_t DisplayDevice::beginFrame(bool mustRecompose) const {
245 return mDisplaySurface->beginFrame(mustRecompose);
Jesse Hall028dc8f2013-08-20 16:35:32 -0700246}
247
Dan Stoza9e56aa02015-11-02 13:00:03 -0800248#ifdef USE_HWC2
249status_t DisplayDevice::prepareFrame(HWComposer& hwc) {
250 status_t error = hwc.prepare(*this);
251 if (error != NO_ERROR) {
252 return error;
253 }
254
255 DisplaySurface::CompositionType compositionType;
256 bool hasClient = hwc.hasClientComposition(mHwcDisplayId);
257 bool hasDevice = hwc.hasDeviceComposition(mHwcDisplayId);
258 if (hasClient && hasDevice) {
259 compositionType = DisplaySurface::COMPOSITION_MIXED;
260 } else if (hasClient) {
261 compositionType = DisplaySurface::COMPOSITION_GLES;
262 } else if (hasDevice) {
263 compositionType = DisplaySurface::COMPOSITION_HWC;
264 } else {
265 // Nothing to do -- when turning the screen off we get a frame like
266 // this. Call it a HWC frame since we won't be doing any GLES work but
267 // will do a prepare/set cycle.
268 compositionType = DisplaySurface::COMPOSITION_HWC;
269 }
270 return mDisplaySurface->prepareFrame(compositionType);
271}
272#else
Jesse Hall38efe862013-04-06 23:12:29 -0700273status_t DisplayDevice::prepareFrame(const HWComposer& hwc) const {
274 DisplaySurface::CompositionType compositionType;
275 bool haveGles = hwc.hasGlesComposition(mHwcDisplayId);
276 bool haveHwc = hwc.hasHwcComposition(mHwcDisplayId);
277 if (haveGles && haveHwc) {
278 compositionType = DisplaySurface::COMPOSITION_MIXED;
279 } else if (haveGles) {
280 compositionType = DisplaySurface::COMPOSITION_GLES;
281 } else if (haveHwc) {
282 compositionType = DisplaySurface::COMPOSITION_HWC;
283 } else {
284 // Nothing to do -- when turning the screen off we get a frame like
285 // this. Call it a HWC frame since we won't be doing any GLES work but
286 // will do a prepare/set cycle.
287 compositionType = DisplaySurface::COMPOSITION_HWC;
288 }
289 return mDisplaySurface->prepareFrame(compositionType);
290}
Dan Stoza9e56aa02015-11-02 13:00:03 -0800291#endif
Jesse Hall38efe862013-04-06 23:12:29 -0700292
Mathias Agopianda27af92012-09-13 18:17:13 -0700293void DisplayDevice::swapBuffers(HWComposer& hwc) const {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800294#ifdef USE_HWC2
295 if (hwc.hasClientComposition(mHwcDisplayId)) {
296#else
Andy McFadden22a99f02013-08-20 16:04:47 -0700297 // We need to call eglSwapBuffers() if:
298 // (1) we don't have a hardware composer, or
299 // (2) we did GLES composition this frame, and either
300 // (a) we have framebuffer target support (not present on legacy
301 // devices, where HWComposer::commit() handles things); or
302 // (b) this is a virtual display
Jesse Hall99c7dbb2013-03-14 14:29:29 -0700303 if (hwc.initCheck() != NO_ERROR ||
304 (hwc.hasGlesComposition(mHwcDisplayId) &&
Andy McFadden22a99f02013-08-20 16:04:47 -0700305 (hwc.supportsFramebufferTarget() || mType >= DISPLAY_VIRTUAL))) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800306#endif
Jesse Hall99c7dbb2013-03-14 14:29:29 -0700307 EGLBoolean success = eglSwapBuffers(mDisplay, mSurface);
308 if (!success) {
309 EGLint error = eglGetError();
310 if (error == EGL_CONTEXT_LOST ||
311 mType == DisplayDevice::DISPLAY_PRIMARY) {
312 LOG_ALWAYS_FATAL("eglSwapBuffers(%p, %p) failed with 0x%08x",
313 mDisplay, mSurface, error);
314 } else {
315 ALOGE("eglSwapBuffers(%p, %p) failed with 0x%08x",
316 mDisplay, mSurface, error);
Mathias Agopianda27af92012-09-13 18:17:13 -0700317 }
318 }
319 }
Mathias Agopian52e21482012-09-24 18:07:21 -0700320
Jesse Hall99c7dbb2013-03-14 14:29:29 -0700321 status_t result = mDisplaySurface->advanceFrame();
322 if (result != NO_ERROR) {
323 ALOGE("[%s] failed pushing new frame to HWC: %d",
324 mDisplayName.string(), result);
Mathias Agopian32341382012-09-25 19:16:28 -0700325 }
Mathias Agopianda27af92012-09-13 18:17:13 -0700326}
327
Dan Stoza9e56aa02015-11-02 13:00:03 -0800328#ifdef USE_HWC2
329void DisplayDevice::onSwapBuffersCompleted() const {
330 mDisplaySurface->onFrameCommitted();
331}
332#else
Mathias Agopianda27af92012-09-13 18:17:13 -0700333void DisplayDevice::onSwapBuffersCompleted(HWComposer& hwc) const {
334 if (hwc.initCheck() == NO_ERROR) {
Jesse Hall851cfe82013-03-20 13:44:00 -0700335 mDisplaySurface->onFrameCommitted();
Mathias Agopianda27af92012-09-13 18:17:13 -0700336 }
337}
Dan Stoza9e56aa02015-11-02 13:00:03 -0800338#endif
Mathias Agopianda27af92012-09-13 18:17:13 -0700339
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700340uint32_t DisplayDevice::getFlags() const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800341{
342 return mFlags;
343}
344
Mathias Agopian875d8e12013-06-07 15:35:48 -0700345EGLBoolean DisplayDevice::makeCurrent(EGLDisplay dpy, EGLContext ctx) const {
Mathias Agopianda8d0a52012-09-04 15:05:38 -0700346 EGLBoolean result = EGL_TRUE;
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700347 EGLSurface sur = eglGetCurrentSurface(EGL_DRAW);
Mathias Agopian875d8e12013-06-07 15:35:48 -0700348 if (sur != mSurface) {
349 result = eglMakeCurrent(dpy, mSurface, mSurface, ctx);
Mathias Agopianda8d0a52012-09-04 15:05:38 -0700350 if (result == EGL_TRUE) {
Jesse Hallf460f552013-08-06 17:08:53 -0700351 if (mType >= DisplayDevice::DISPLAY_VIRTUAL)
352 eglSwapInterval(dpy, 0);
Mathias Agopianda8d0a52012-09-04 15:05:38 -0700353 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700354 }
Mathias Agopian931bda12013-08-28 18:11:46 -0700355 setViewportAndProjection();
Mathias Agopianda8d0a52012-09-04 15:05:38 -0700356 return result;
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700357}
358
Mathias Agopian875d8e12013-06-07 15:35:48 -0700359void DisplayDevice::setViewportAndProjection() const {
360 size_t w = mDisplayWidth;
361 size_t h = mDisplayHeight;
Dan Stozac1879002014-05-22 15:59:05 -0700362 Rect sourceCrop(0, 0, w, h);
Riley Andrewsc3ebe662014-09-04 16:20:31 -0700363 mFlinger->getRenderEngine().setViewportAndProjection(w, h, sourceCrop, h,
364 false, Transform::ROT_0);
Mathias Agopianbae92d02012-09-28 01:00:47 -0700365}
366
Dan Stoza9e56aa02015-11-02 13:00:03 -0800367#ifdef USE_HWC2
368const sp<Fence>& DisplayDevice::getClientTargetAcquireFence() const {
369 return mDisplaySurface->getClientTargetAcquireFence();
370}
371#endif
372
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// ----------------------------------------------------------------------------
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700418
Mathias Agopian28947d72012-08-08 18:51:15 -0700419void DisplayDevice::setLayerStack(uint32_t stack) {
420 mLayerStack = stack;
421 dirtyRegion.set(bounds());
422}
423
424// ----------------------------------------------------------------------------
425
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700426uint32_t DisplayDevice::getOrientationTransform() const {
427 uint32_t transform = 0;
428 switch (mOrientation) {
429 case DisplayState::eOrientationDefault:
430 transform = Transform::ROT_0;
431 break;
432 case DisplayState::eOrientation90:
433 transform = Transform::ROT_90;
434 break;
435 case DisplayState::eOrientation180:
436 transform = Transform::ROT_180;
437 break;
438 case DisplayState::eOrientation270:
439 transform = Transform::ROT_270;
440 break;
441 }
442 return transform;
443}
444
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700445status_t DisplayDevice::orientationToTransfrom(
Mathias Agopian1b031492012-06-20 17:51:20 -0700446 int orientation, int w, int h, Transform* tr)
447{
448 uint32_t flags = 0;
449 switch (orientation) {
Mathias Agopian3165cc22012-08-08 19:42:09 -0700450 case DisplayState::eOrientationDefault:
Mathias Agopian1b031492012-06-20 17:51:20 -0700451 flags = Transform::ROT_0;
452 break;
Mathias Agopian3165cc22012-08-08 19:42:09 -0700453 case DisplayState::eOrientation90:
Mathias Agopian1b031492012-06-20 17:51:20 -0700454 flags = Transform::ROT_90;
455 break;
Mathias Agopian3165cc22012-08-08 19:42:09 -0700456 case DisplayState::eOrientation180:
Mathias Agopian1b031492012-06-20 17:51:20 -0700457 flags = Transform::ROT_180;
458 break;
Mathias Agopian3165cc22012-08-08 19:42:09 -0700459 case DisplayState::eOrientation270:
Mathias Agopian1b031492012-06-20 17:51:20 -0700460 flags = Transform::ROT_270;
461 break;
462 default:
463 return BAD_VALUE;
464 }
465 tr->set(flags, w, h);
466 return NO_ERROR;
467}
468
Michael Lentine47e45402014-07-18 15:34:25 -0700469void DisplayDevice::setDisplaySize(const int newWidth, const int newHeight) {
470 dirtyRegion.set(getBounds());
471
Michael Lentinef2568de2014-08-20 10:51:23 -0700472 if (mSurface != EGL_NO_SURFACE) {
473 eglDestroySurface(mDisplay, mSurface);
474 mSurface = EGL_NO_SURFACE;
475 }
476
Michael Lentine47e45402014-07-18 15:34:25 -0700477 mDisplaySurface->resizeBuffers(newWidth, newHeight);
478
479 ANativeWindow* const window = mNativeWindow.get();
480 mSurface = eglCreateWindowSurface(mDisplay, mConfig, window, NULL);
481 eglQuerySurface(mDisplay, mSurface, EGL_WIDTH, &mDisplayWidth);
482 eglQuerySurface(mDisplay, mSurface, EGL_HEIGHT, &mDisplayHeight);
483
484 LOG_FATAL_IF(mDisplayWidth != newWidth,
485 "Unable to set new width to %d", newWidth);
486 LOG_FATAL_IF(mDisplayHeight != newHeight,
487 "Unable to set new height to %d", newHeight);
488}
489
Mathias Agopian00e8c7a2012-09-04 19:30:46 -0700490void DisplayDevice::setProjection(int orientation,
Mathias Agopianf5f714a2013-02-26 16:54:05 -0800491 const Rect& newViewport, const Rect& newFrame) {
492 Rect viewport(newViewport);
493 Rect frame(newFrame);
494
495 const int w = mDisplayWidth;
496 const int h = mDisplayHeight;
497
498 Transform R;
499 DisplayDevice::orientationToTransfrom(orientation, w, h, &R);
500
501 if (!frame.isValid()) {
502 // the destination frame can be invalid if it has never been set,
503 // in that case we assume the whole display frame.
504 frame = Rect(w, h);
505 }
506
507 if (viewport.isEmpty()) {
508 // viewport can be invalid if it has never been set, in that case
509 // we assume the whole display size.
510 // it's also invalid to have an empty viewport, so we handle that
511 // case in the same way.
512 viewport = Rect(w, h);
513 if (R.getOrientation() & Transform::ROT_90) {
514 // viewport is always specified in the logical orientation
515 // of the display (ie: post-rotation).
516 swap(viewport.right, viewport.bottom);
517 }
518 }
519
520 dirtyRegion.set(getBounds());
521
522 Transform TL, TP, S;
523 float src_width = viewport.width();
524 float src_height = viewport.height();
525 float dst_width = frame.width();
526 float dst_height = frame.height();
527 if (src_width != dst_width || src_height != dst_height) {
528 float sx = dst_width / src_width;
529 float sy = dst_height / src_height;
530 S.set(sx, 0, 0, sy);
531 }
532
533 float src_x = viewport.left;
534 float src_y = viewport.top;
535 float dst_x = frame.left;
536 float dst_y = frame.top;
537 TL.set(-src_x, -src_y);
538 TP.set(dst_x, dst_y);
539
540 // The viewport and frame are both in the logical orientation.
541 // Apply the logical translation, scale to physical size, apply the
542 // physical translation and finally rotate to the physical orientation.
543 mGlobalTransform = R * TP * S * TL;
544
545 const uint8_t type = mGlobalTransform.getType();
546 mNeedsFiltering = (!mGlobalTransform.preserveRects() ||
547 (type >= Transform::SCALE));
548
549 mScissor = mGlobalTransform.transform(viewport);
550 if (mScissor.isEmpty()) {
Mathias Agopian6c7f25a2013-05-09 20:37:10 -0700551 mScissor = getBounds();
Mathias Agopianf5f714a2013-02-26 16:54:05 -0800552 }
553
Mathias Agopianda8d0a52012-09-04 15:05:38 -0700554 mOrientation = orientation;
Mathias Agopian00e8c7a2012-09-04 19:30:46 -0700555 mViewport = viewport;
556 mFrame = frame;
Mathias Agopian1b031492012-06-20 17:51:20 -0700557}
Mathias Agopian1d12d8a2012-09-18 01:38:00 -0700558
Mathias Agopian74d211a2013-04-22 16:55:35 +0200559void DisplayDevice::dump(String8& result) const {
Mathias Agopian1d12d8a2012-09-18 01:38:00 -0700560 const Transform& tr(mGlobalTransform);
Mathias Agopian74d211a2013-04-22 16:55:35 +0200561 result.appendFormat(
Mathias Agopian1d12d8a2012-09-18 01:38:00 -0700562 "+ DisplayDevice: %s\n"
Jesse Hall02d86562013-03-25 14:43:23 -0700563 " type=%x, hwcId=%d, layerStack=%u, (%4dx%4d), ANativeWindow=%p, orient=%2d (type=%08x), "
Pablo Ceballosb5b35632016-02-23 11:18:51 -0800564 "flips=%u, isSecure=%d, powerMode=%d, activeConfig=%d, numLayers=%zu\n"
Mathias Agopian766dc492012-10-30 18:08:06 -0700565 " v:[%d,%d,%d,%d], f:[%d,%d,%d,%d], s:[%d,%d,%d,%d],"
Mathias Agopian1d12d8a2012-09-18 01:38:00 -0700566 "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 -0700567 mDisplayName.string(), mType, mHwcDisplayId,
Mathias Agopian1d12d8a2012-09-18 01:38:00 -0700568 mLayerStack, mDisplayWidth, mDisplayHeight, mNativeWindow.get(),
569 mOrientation, tr.getType(), getPageFlipCount(),
Pablo Ceballosb5b35632016-02-23 11:18:51 -0800570 mIsSecure, mPowerMode, mActiveConfig,
Michael Lentine6c9e34a2014-07-14 13:48:55 -0700571 mVisibleLayersSortedByZ.size(),
Mathias Agopian1d12d8a2012-09-18 01:38:00 -0700572 mViewport.left, mViewport.top, mViewport.right, mViewport.bottom,
573 mFrame.left, mFrame.top, mFrame.right, mFrame.bottom,
Mathias Agopian766dc492012-10-30 18:08:06 -0700574 mScissor.left, mScissor.top, mScissor.right, mScissor.bottom,
Mathias Agopian1d12d8a2012-09-18 01:38:00 -0700575 tr[0][0], tr[1][0], tr[2][0],
576 tr[0][1], tr[1][1], tr[2][1],
577 tr[0][2], tr[1][2], tr[2][2]);
578
Jesse Hall99c7dbb2013-03-14 14:29:29 -0700579 String8 surfaceDump;
Dan Stozaf10c46e2014-11-11 10:32:31 -0800580 mDisplaySurface->dumpAsString(surfaceDump);
Jesse Hall99c7dbb2013-03-14 14:29:29 -0700581 result.append(surfaceDump);
Mathias Agopian1d12d8a2012-09-18 01:38:00 -0700582}