blob: 43fb19dc0728a57635a6f04391022b99744a9c02 [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#include "DisplayHardware/HWC2.h"
Mathias Agopian875d8e12013-06-07 15:35:48 -070041#include "RenderEngine/RenderEngine.h"
Mathias Agopian1b031492012-06-20 17:51:20 -070042
Mathias Agopianda8d0a52012-09-04 15:05:38 -070043#include "clz.h"
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -070044#include "DisplayDevice.h"
Mathias Agopianc7d14e22011-08-01 16:32:21 -070045#include "SurfaceFlinger.h"
Mathias Agopian13127d82013-03-05 17:47:11 -080046#include "Layer.h"
Mathias Agopian1f7bec62010-06-25 18:02:21 -070047
Mathias Agopiana4912602012-07-12 14:25:33 -070048// ----------------------------------------------------------------------------
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080049using namespace android;
Mathias Agopiana4912602012-07-12 14:25:33 -070050// ----------------------------------------------------------------------------
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080051
Andreas Gampe89fd4f72014-11-13 14:18:56 -080052#ifdef EGL_ANDROID_swap_rectangle
53static constexpr bool kEGLAndroidSwapRectangle = true;
54#else
55static constexpr bool kEGLAndroidSwapRectangle = false;
56#endif
57
58#if !defined(EGL_EGLEXT_PROTOTYPES) || !defined(EGL_ANDROID_swap_rectangle)
59// Dummy implementation in case it is missing.
60inline void eglSetSwapRectangleANDROID (EGLDisplay, EGLSurface, EGLint, EGLint, EGLint, EGLint) {
61}
62#endif
63
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080064/*
65 * Initialize the display to the specified values.
66 *
67 */
68
Pablo Ceballos021623b2016-04-15 17:31:51 -070069uint32_t DisplayDevice::sPrimaryDisplayOrientation = 0;
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,
Jamie Gennisdd3cb842012-10-19 18:19:11 -070075 bool isSecure,
76 const wp<IBinder>& displayToken,
Jesse Hall99c7dbb2013-03-14 14:29:29 -070077 const sp<DisplaySurface>& displaySurface,
Mathias Agopiandb89edc2013-08-02 01:40:18 -070078 const sp<IGraphicBufferProducer>& producer,
Mathias Agopiana4912602012-07-12 14:25:33 -070079 EGLConfig config)
Jesse Hallb7a05492014-08-14 15:45:06 -070080 : lastCompositionHadVisibleLayers(false),
81 mFlinger(flinger),
Dan Stoza9e56aa02015-11-02 13:00:03 -080082 mType(type),
83 mHwcDisplayId(hwcId),
Chih-Wei Huang27e25622013-01-07 17:33:56 +080084 mDisplayToken(displayToken),
Jesse Hall99c7dbb2013-03-14 14:29:29 -070085 mDisplaySurface(displaySurface),
Mathias Agopian92a979a2012-08-02 18:32:23 -070086 mDisplay(EGL_NO_DISPLAY),
87 mSurface(EGL_NO_SURFACE),
Dan Stoza9e56aa02015-11-02 13:00:03 -080088 mDisplayWidth(),
89 mDisplayHeight(),
Mathias Agopian92a979a2012-08-02 18:32:23 -070090 mFlags(),
91 mPageFlipCount(),
Jamie Gennisdd3cb842012-10-19 18:19:11 -070092 mIsSecure(isSecure),
Jesse Hall01e29052013-02-19 16:13:35 -080093 mLayerStack(NO_LAYER_STACK),
Prashant Malani2c9b11f2014-05-25 01:36:31 -070094 mOrientation(),
Michael Lentine6c9e34a2014-07-14 13:48:55 -070095 mPowerMode(HWC_POWER_MODE_OFF),
96 mActiveConfig(0)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080097{
james.zhang5e8eb5e2015-12-01 17:55:11 +080098 Surface* surface;
99 mNativeWindow = surface = new Surface(producer, false);
Jesse Hallffe1f192013-03-22 15:13:48 -0700100 ANativeWindow* const window = mNativeWindow.get();
101
Jesse Hallffe1f192013-03-22 15:13:48 -0700102 /*
103 * Create our display's surface
104 */
105
james.zhang5e8eb5e2015-12-01 17:55:11 +0800106 EGLSurface eglSurface;
Jesse Hallffe1f192013-03-22 15:13:48 -0700107 EGLDisplay display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
Jesse Hall19e87292013-12-23 21:02:15 -0800108 if (config == EGL_NO_CONFIG) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800109 config = RenderEngine::chooseEglConfig(display, PIXEL_FORMAT_RGBA_8888);
Jesse Hall19e87292013-12-23 21:02:15 -0800110 }
james.zhang5e8eb5e2015-12-01 17:55:11 +0800111 eglSurface = eglCreateWindowSurface(display, config, window, NULL);
112 eglQuerySurface(display, eglSurface, EGL_WIDTH, &mDisplayWidth);
113 eglQuerySurface(display, eglSurface, EGL_HEIGHT, &mDisplayHeight);
Jesse Hallffe1f192013-03-22 15:13:48 -0700114
John Dong4ee56962014-02-21 12:37:59 -0800115 // Make sure that composition can never be stalled by a virtual display
116 // consumer that isn't processing buffers fast enough. We have to do this
117 // in two places:
118 // * Here, in case the display is composed entirely by HWC.
119 // * In makeCurrent(), using eglSwapInterval. Some EGL drivers set the
120 // window's swap interval in eglMakeCurrent, so they'll override the
121 // interval we set here.
122 if (mType >= DisplayDevice::DISPLAY_VIRTUAL)
123 window->setSwapInterval(window, 0);
124
Michael Lentine47e45402014-07-18 15:34:25 -0700125 mConfig = config;
Jesse Hallffe1f192013-03-22 15:13:48 -0700126 mDisplay = display;
james.zhang5e8eb5e2015-12-01 17:55:11 +0800127 mSurface = eglSurface;
Jesse Hallffe1f192013-03-22 15:13:48 -0700128 mPageFlipCount = 0;
129 mViewport.makeInvalid();
130 mFrame.makeInvalid();
131
132 // virtual displays are always considered enabled
Prashant Malani2c9b11f2014-05-25 01:36:31 -0700133 mPowerMode = (mType >= DisplayDevice::DISPLAY_VIRTUAL) ?
134 HWC_POWER_MODE_NORMAL : HWC_POWER_MODE_OFF;
Jesse Hallffe1f192013-03-22 15:13:48 -0700135
136 // Name the display. The name will be replaced shortly if the display
137 // was created with createDisplay().
138 switch (mType) {
139 case DISPLAY_PRIMARY:
140 mDisplayName = "Built-in Screen";
141 break;
142 case DISPLAY_EXTERNAL:
143 mDisplayName = "HDMI Screen";
144 break;
145 default:
146 mDisplayName = "Virtual Screen"; // e.g. Overlay #n
147 break;
148 }
149
150 // initialize the display orientation transform.
151 setProjection(DisplayState::eOrientationDefault, mViewport, mFrame);
james.zhang5e8eb5e2015-12-01 17:55:11 +0800152
153#ifdef NUM_FRAMEBUFFER_SURFACE_BUFFERS
154 surface->allocateBuffers();
155#endif
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800156}
157
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700158DisplayDevice::~DisplayDevice() {
Mathias Agopian92a979a2012-08-02 18:32:23 -0700159 if (mSurface != EGL_NO_SURFACE) {
160 eglDestroySurface(mDisplay, mSurface);
161 mSurface = EGL_NO_SURFACE;
162 }
163}
164
Jesse Hall02d86562013-03-25 14:43:23 -0700165void DisplayDevice::disconnect(HWComposer& hwc) {
166 if (mHwcDisplayId >= 0) {
167 hwc.disconnectDisplay(mHwcDisplayId);
Jesse Hall02d86562013-03-25 14:43:23 -0700168 mHwcDisplayId = -1;
169 }
170}
171
Mathias Agopian92a979a2012-08-02 18:32:23 -0700172bool DisplayDevice::isValid() const {
173 return mFlinger != NULL;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800174}
175
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700176int DisplayDevice::getWidth() const {
Mathias Agopiana4912602012-07-12 14:25:33 -0700177 return mDisplayWidth;
178}
179
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700180int DisplayDevice::getHeight() const {
Mathias Agopiana4912602012-07-12 14:25:33 -0700181 return mDisplayHeight;
182}
183
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700184EGLSurface DisplayDevice::getEGLSurface() const {
Mathias Agopiana4912602012-07-12 14:25:33 -0700185 return mSurface;
186}
187
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700188void DisplayDevice::setDisplayName(const String8& displayName) {
189 if (!displayName.isEmpty()) {
190 // never override the name with an empty name
191 mDisplayName = displayName;
192 }
193}
194
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700195uint32_t DisplayDevice::getPageFlipCount() const {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700196 return mPageFlipCount;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800197}
198
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700199void DisplayDevice::flip(const Region& dirty) const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800200{
Mathias Agopian875d8e12013-06-07 15:35:48 -0700201 mFlinger->getRenderEngine().checkErrors();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800202
Andreas Gampe89fd4f72014-11-13 14:18:56 -0800203 if (kEGLAndroidSwapRectangle) {
204 if (mFlags & SWAP_RECTANGLE) {
205 const Region newDirty(dirty.intersect(bounds()));
206 const Rect b(newDirty.getBounds());
207 eglSetSwapRectangleANDROID(mDisplay, mSurface,
208 b.left, b.top, b.width(), b.height());
209 }
Jesse Hall01e29052013-02-19 16:13:35 -0800210 }
Mathias Agopiand8707032012-09-18 01:21:55 -0700211
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700212 mPageFlipCount++;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800213}
214
Dan Stoza71433162014-02-04 16:22:36 -0800215status_t DisplayDevice::beginFrame(bool mustRecompose) const {
216 return mDisplaySurface->beginFrame(mustRecompose);
Jesse Hall028dc8f2013-08-20 16:35:32 -0700217}
218
Dan Stoza9e56aa02015-11-02 13:00:03 -0800219status_t DisplayDevice::prepareFrame(HWComposer& hwc) {
220 status_t error = hwc.prepare(*this);
221 if (error != NO_ERROR) {
222 return error;
223 }
224
225 DisplaySurface::CompositionType compositionType;
226 bool hasClient = hwc.hasClientComposition(mHwcDisplayId);
227 bool hasDevice = hwc.hasDeviceComposition(mHwcDisplayId);
228 if (hasClient && hasDevice) {
229 compositionType = DisplaySurface::COMPOSITION_MIXED;
230 } else if (hasClient) {
231 compositionType = DisplaySurface::COMPOSITION_GLES;
232 } else if (hasDevice) {
233 compositionType = DisplaySurface::COMPOSITION_HWC;
234 } else {
235 // Nothing to do -- when turning the screen off we get a frame like
236 // this. Call it a HWC frame since we won't be doing any GLES work but
237 // will do a prepare/set cycle.
238 compositionType = DisplaySurface::COMPOSITION_HWC;
239 }
240 return mDisplaySurface->prepareFrame(compositionType);
241}
Jesse Hall38efe862013-04-06 23:12:29 -0700242
Mathias Agopianda27af92012-09-13 18:17:13 -0700243void DisplayDevice::swapBuffers(HWComposer& hwc) const {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800244 if (hwc.hasClientComposition(mHwcDisplayId)) {
Jesse Hall99c7dbb2013-03-14 14:29:29 -0700245 EGLBoolean success = eglSwapBuffers(mDisplay, mSurface);
246 if (!success) {
247 EGLint error = eglGetError();
248 if (error == EGL_CONTEXT_LOST ||
249 mType == DisplayDevice::DISPLAY_PRIMARY) {
250 LOG_ALWAYS_FATAL("eglSwapBuffers(%p, %p) failed with 0x%08x",
251 mDisplay, mSurface, error);
252 } else {
253 ALOGE("eglSwapBuffers(%p, %p) failed with 0x%08x",
254 mDisplay, mSurface, error);
Mathias Agopianda27af92012-09-13 18:17:13 -0700255 }
256 }
257 }
Mathias Agopian52e21482012-09-24 18:07:21 -0700258
Jesse Hall99c7dbb2013-03-14 14:29:29 -0700259 status_t result = mDisplaySurface->advanceFrame();
260 if (result != NO_ERROR) {
261 ALOGE("[%s] failed pushing new frame to HWC: %d",
262 mDisplayName.string(), result);
Mathias Agopian32341382012-09-25 19:16:28 -0700263 }
Mathias Agopianda27af92012-09-13 18:17:13 -0700264}
265
Dan Stoza9e56aa02015-11-02 13:00:03 -0800266void DisplayDevice::onSwapBuffersCompleted() const {
267 mDisplaySurface->onFrameCommitted();
268}
Mathias Agopianda27af92012-09-13 18:17:13 -0700269
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700270uint32_t DisplayDevice::getFlags() const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800271{
272 return mFlags;
273}
274
Mathias Agopian875d8e12013-06-07 15:35:48 -0700275EGLBoolean DisplayDevice::makeCurrent(EGLDisplay dpy, EGLContext ctx) const {
Mathias Agopianda8d0a52012-09-04 15:05:38 -0700276 EGLBoolean result = EGL_TRUE;
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700277 EGLSurface sur = eglGetCurrentSurface(EGL_DRAW);
Mathias Agopian875d8e12013-06-07 15:35:48 -0700278 if (sur != mSurface) {
279 result = eglMakeCurrent(dpy, mSurface, mSurface, ctx);
Mathias Agopianda8d0a52012-09-04 15:05:38 -0700280 if (result == EGL_TRUE) {
Jesse Hallf460f552013-08-06 17:08:53 -0700281 if (mType >= DisplayDevice::DISPLAY_VIRTUAL)
282 eglSwapInterval(dpy, 0);
Mathias Agopianda8d0a52012-09-04 15:05:38 -0700283 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700284 }
Mathias Agopian931bda12013-08-28 18:11:46 -0700285 setViewportAndProjection();
Mathias Agopianda8d0a52012-09-04 15:05:38 -0700286 return result;
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700287}
288
Mathias Agopian875d8e12013-06-07 15:35:48 -0700289void DisplayDevice::setViewportAndProjection() const {
290 size_t w = mDisplayWidth;
291 size_t h = mDisplayHeight;
Dan Stozac1879002014-05-22 15:59:05 -0700292 Rect sourceCrop(0, 0, w, h);
Riley Andrewsc3ebe662014-09-04 16:20:31 -0700293 mFlinger->getRenderEngine().setViewportAndProjection(w, h, sourceCrop, h,
294 false, Transform::ROT_0);
Mathias Agopianbae92d02012-09-28 01:00:47 -0700295}
296
Dan Stoza9e56aa02015-11-02 13:00:03 -0800297const sp<Fence>& DisplayDevice::getClientTargetAcquireFence() const {
298 return mDisplaySurface->getClientTargetAcquireFence();
299}
Dan Stoza9e56aa02015-11-02 13:00:03 -0800300
Mathias Agopian1b031492012-06-20 17:51:20 -0700301// ----------------------------------------------------------------------------
302
Mathias Agopian13127d82013-03-05 17:47:11 -0800303void DisplayDevice::setVisibleLayersSortedByZ(const Vector< sp<Layer> >& layers) {
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700304 mVisibleLayersSortedByZ = layers;
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700305}
306
Mathias Agopian13127d82013-03-05 17:47:11 -0800307const Vector< sp<Layer> >& DisplayDevice::getVisibleLayersSortedByZ() const {
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700308 return mVisibleLayersSortedByZ;
309}
310
Mathias Agopiancd60f992012-08-16 16:28:27 -0700311Region DisplayDevice::getDirtyRegion(bool repaintEverything) const {
312 Region dirty;
Mathias Agopiancd60f992012-08-16 16:28:27 -0700313 if (repaintEverything) {
314 dirty.set(getBounds());
315 } else {
Mathias Agopianda8d0a52012-09-04 15:05:38 -0700316 const Transform& planeTransform(mGlobalTransform);
Mathias Agopiancd60f992012-08-16 16:28:27 -0700317 dirty = planeTransform.transform(this->dirtyRegion);
318 dirty.andSelf(getBounds());
319 }
320 return dirty;
321}
322
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700323// ----------------------------------------------------------------------------
Prashant Malani2c9b11f2014-05-25 01:36:31 -0700324void DisplayDevice::setPowerMode(int mode) {
325 mPowerMode = mode;
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700326}
327
Prashant Malani2c9b11f2014-05-25 01:36:31 -0700328int DisplayDevice::getPowerMode() const {
329 return mPowerMode;
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700330}
331
Prashant Malani2c9b11f2014-05-25 01:36:31 -0700332bool DisplayDevice::isDisplayOn() const {
333 return (mPowerMode != HWC_POWER_MODE_OFF);
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700334}
335
336// ----------------------------------------------------------------------------
Michael Lentine6c9e34a2014-07-14 13:48:55 -0700337void DisplayDevice::setActiveConfig(int mode) {
338 mActiveConfig = mode;
339}
340
341int DisplayDevice::getActiveConfig() const {
342 return mActiveConfig;
343}
344
345// ----------------------------------------------------------------------------
Michael Wright28f24d02016-07-12 13:30:53 -0700346void DisplayDevice::setActiveColorMode(android_color_mode_t mode) {
347 mActiveColorMode = mode;
348}
349
350android_color_mode_t DisplayDevice::getActiveColorMode() const {
351 return mActiveColorMode;
352}
Michael Wright28f24d02016-07-12 13:30:53 -0700353
354// ----------------------------------------------------------------------------
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700355
Mathias Agopian28947d72012-08-08 18:51:15 -0700356void DisplayDevice::setLayerStack(uint32_t stack) {
357 mLayerStack = stack;
358 dirtyRegion.set(bounds());
359}
360
361// ----------------------------------------------------------------------------
362
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700363uint32_t DisplayDevice::getOrientationTransform() const {
364 uint32_t transform = 0;
365 switch (mOrientation) {
366 case DisplayState::eOrientationDefault:
367 transform = Transform::ROT_0;
368 break;
369 case DisplayState::eOrientation90:
370 transform = Transform::ROT_90;
371 break;
372 case DisplayState::eOrientation180:
373 transform = Transform::ROT_180;
374 break;
375 case DisplayState::eOrientation270:
376 transform = Transform::ROT_270;
377 break;
378 }
379 return transform;
380}
381
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700382status_t DisplayDevice::orientationToTransfrom(
Mathias Agopian1b031492012-06-20 17:51:20 -0700383 int orientation, int w, int h, Transform* tr)
384{
385 uint32_t flags = 0;
386 switch (orientation) {
Mathias Agopian3165cc22012-08-08 19:42:09 -0700387 case DisplayState::eOrientationDefault:
Mathias Agopian1b031492012-06-20 17:51:20 -0700388 flags = Transform::ROT_0;
389 break;
Mathias Agopian3165cc22012-08-08 19:42:09 -0700390 case DisplayState::eOrientation90:
Mathias Agopian1b031492012-06-20 17:51:20 -0700391 flags = Transform::ROT_90;
392 break;
Mathias Agopian3165cc22012-08-08 19:42:09 -0700393 case DisplayState::eOrientation180:
Mathias Agopian1b031492012-06-20 17:51:20 -0700394 flags = Transform::ROT_180;
395 break;
Mathias Agopian3165cc22012-08-08 19:42:09 -0700396 case DisplayState::eOrientation270:
Mathias Agopian1b031492012-06-20 17:51:20 -0700397 flags = Transform::ROT_270;
398 break;
399 default:
400 return BAD_VALUE;
401 }
402 tr->set(flags, w, h);
403 return NO_ERROR;
404}
405
Michael Lentine47e45402014-07-18 15:34:25 -0700406void DisplayDevice::setDisplaySize(const int newWidth, const int newHeight) {
407 dirtyRegion.set(getBounds());
408
Michael Lentinef2568de2014-08-20 10:51:23 -0700409 if (mSurface != EGL_NO_SURFACE) {
410 eglDestroySurface(mDisplay, mSurface);
411 mSurface = EGL_NO_SURFACE;
412 }
413
Michael Lentine47e45402014-07-18 15:34:25 -0700414 mDisplaySurface->resizeBuffers(newWidth, newHeight);
415
416 ANativeWindow* const window = mNativeWindow.get();
417 mSurface = eglCreateWindowSurface(mDisplay, mConfig, window, NULL);
418 eglQuerySurface(mDisplay, mSurface, EGL_WIDTH, &mDisplayWidth);
419 eglQuerySurface(mDisplay, mSurface, EGL_HEIGHT, &mDisplayHeight);
420
421 LOG_FATAL_IF(mDisplayWidth != newWidth,
422 "Unable to set new width to %d", newWidth);
423 LOG_FATAL_IF(mDisplayHeight != newHeight,
424 "Unable to set new height to %d", newHeight);
425}
426
Mathias Agopian00e8c7a2012-09-04 19:30:46 -0700427void DisplayDevice::setProjection(int orientation,
Mathias Agopianf5f714a2013-02-26 16:54:05 -0800428 const Rect& newViewport, const Rect& newFrame) {
429 Rect viewport(newViewport);
430 Rect frame(newFrame);
431
432 const int w = mDisplayWidth;
433 const int h = mDisplayHeight;
434
435 Transform R;
436 DisplayDevice::orientationToTransfrom(orientation, w, h, &R);
437
438 if (!frame.isValid()) {
439 // the destination frame can be invalid if it has never been set,
440 // in that case we assume the whole display frame.
441 frame = Rect(w, h);
442 }
443
444 if (viewport.isEmpty()) {
445 // viewport can be invalid if it has never been set, in that case
446 // we assume the whole display size.
447 // it's also invalid to have an empty viewport, so we handle that
448 // case in the same way.
449 viewport = Rect(w, h);
450 if (R.getOrientation() & Transform::ROT_90) {
451 // viewport is always specified in the logical orientation
452 // of the display (ie: post-rotation).
453 swap(viewport.right, viewport.bottom);
454 }
455 }
456
457 dirtyRegion.set(getBounds());
458
459 Transform TL, TP, S;
460 float src_width = viewport.width();
461 float src_height = viewport.height();
462 float dst_width = frame.width();
463 float dst_height = frame.height();
464 if (src_width != dst_width || src_height != dst_height) {
465 float sx = dst_width / src_width;
466 float sy = dst_height / src_height;
467 S.set(sx, 0, 0, sy);
468 }
469
470 float src_x = viewport.left;
471 float src_y = viewport.top;
472 float dst_x = frame.left;
473 float dst_y = frame.top;
474 TL.set(-src_x, -src_y);
475 TP.set(dst_x, dst_y);
476
477 // The viewport and frame are both in the logical orientation.
478 // Apply the logical translation, scale to physical size, apply the
479 // physical translation and finally rotate to the physical orientation.
480 mGlobalTransform = R * TP * S * TL;
481
482 const uint8_t type = mGlobalTransform.getType();
483 mNeedsFiltering = (!mGlobalTransform.preserveRects() ||
484 (type >= Transform::SCALE));
485
486 mScissor = mGlobalTransform.transform(viewport);
487 if (mScissor.isEmpty()) {
Mathias Agopian6c7f25a2013-05-09 20:37:10 -0700488 mScissor = getBounds();
Mathias Agopianf5f714a2013-02-26 16:54:05 -0800489 }
490
Mathias Agopianda8d0a52012-09-04 15:05:38 -0700491 mOrientation = orientation;
Pablo Ceballos021623b2016-04-15 17:31:51 -0700492 if (mType == DisplayType::DISPLAY_PRIMARY) {
493 uint32_t transform = 0;
494 switch (mOrientation) {
495 case DisplayState::eOrientationDefault:
496 transform = Transform::ROT_0;
497 break;
498 case DisplayState::eOrientation90:
499 transform = Transform::ROT_90;
500 break;
501 case DisplayState::eOrientation180:
502 transform = Transform::ROT_180;
503 break;
504 case DisplayState::eOrientation270:
505 transform = Transform::ROT_270;
506 break;
507 }
508 sPrimaryDisplayOrientation = transform;
509 }
Mathias Agopian00e8c7a2012-09-04 19:30:46 -0700510 mViewport = viewport;
511 mFrame = frame;
Mathias Agopian1b031492012-06-20 17:51:20 -0700512}
Mathias Agopian1d12d8a2012-09-18 01:38:00 -0700513
Pablo Ceballos021623b2016-04-15 17:31:51 -0700514uint32_t DisplayDevice::getPrimaryDisplayOrientationTransform() {
515 return sPrimaryDisplayOrientation;
516}
517
Mathias Agopian74d211a2013-04-22 16:55:35 +0200518void DisplayDevice::dump(String8& result) const {
Mathias Agopian1d12d8a2012-09-18 01:38:00 -0700519 const Transform& tr(mGlobalTransform);
Mathias Agopian74d211a2013-04-22 16:55:35 +0200520 result.appendFormat(
Mathias Agopian1d12d8a2012-09-18 01:38:00 -0700521 "+ DisplayDevice: %s\n"
Jesse Hall02d86562013-03-25 14:43:23 -0700522 " type=%x, hwcId=%d, layerStack=%u, (%4dx%4d), ANativeWindow=%p, orient=%2d (type=%08x), "
Pablo Ceballosb5b35632016-02-23 11:18:51 -0800523 "flips=%u, isSecure=%d, powerMode=%d, activeConfig=%d, numLayers=%zu\n"
Mathias Agopian766dc492012-10-30 18:08:06 -0700524 " v:[%d,%d,%d,%d], f:[%d,%d,%d,%d], s:[%d,%d,%d,%d],"
Mathias Agopian1d12d8a2012-09-18 01:38:00 -0700525 "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 -0700526 mDisplayName.string(), mType, mHwcDisplayId,
Mathias Agopian1d12d8a2012-09-18 01:38:00 -0700527 mLayerStack, mDisplayWidth, mDisplayHeight, mNativeWindow.get(),
528 mOrientation, tr.getType(), getPageFlipCount(),
Pablo Ceballosb5b35632016-02-23 11:18:51 -0800529 mIsSecure, mPowerMode, mActiveConfig,
Michael Lentine6c9e34a2014-07-14 13:48:55 -0700530 mVisibleLayersSortedByZ.size(),
Mathias Agopian1d12d8a2012-09-18 01:38:00 -0700531 mViewport.left, mViewport.top, mViewport.right, mViewport.bottom,
532 mFrame.left, mFrame.top, mFrame.right, mFrame.bottom,
Mathias Agopian766dc492012-10-30 18:08:06 -0700533 mScissor.left, mScissor.top, mScissor.right, mScissor.bottom,
Mathias Agopian1d12d8a2012-09-18 01:38:00 -0700534 tr[0][0], tr[1][0], tr[2][0],
535 tr[0][1], tr[1][1], tr[2][1],
536 tr[0][2], tr[1][2], tr[2][2]);
537
Jesse Hall99c7dbb2013-03-14 14:29:29 -0700538 String8 surfaceDump;
Dan Stozaf10c46e2014-11-11 10:32:31 -0800539 mDisplaySurface->dumpAsString(surfaceDump);
Jesse Hall99c7dbb2013-03-14 14:29:29 -0700540 result.append(surfaceDump);
Mathias Agopian1d12d8a2012-09-18 01:38:00 -0700541}
Irvelffc9efc2016-07-27 15:16:37 -0700542
543std::atomic<int32_t> DisplayDeviceState::nextDisplayId(1);
544
545DisplayDeviceState::DisplayDeviceState(DisplayDevice::DisplayType type, bool isSecure)
546 : type(type),
547 layerStack(DisplayDevice::NO_LAYER_STACK),
548 orientation(0),
549 width(0),
550 height(0),
551 isSecure(isSecure)
552{
553 viewport.makeInvalid();
554 frame.makeInvalid();
555}