blob: e9cbbca94ba99737c0a33b24ffa65d4efe9b473e [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
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080017#include <stdlib.h>
18#include <stdio.h>
19#include <string.h>
20#include <math.h>
21
22#include <cutils/properties.h>
23
Mathias Agopian076b1cc2009-04-10 14:24:30 -070024#include <utils/RefBase.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080025#include <utils/Log.h>
26
Mathias Agopianc666cae2012-07-25 18:56:13 -070027#include <ui/DisplayInfo.h>
Mathias Agopian076b1cc2009-04-10 14:24:30 -070028#include <ui/PixelFormat.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080029
Mathias Agopiane3c697f2013-02-14 17:11:02 -080030#include <gui/Surface.h>
Jamie Gennis1a4d8832012-08-02 20:11:05 -070031
Mathias Agopian076b1cc2009-04-10 14:24:30 -070032#include <hardware/gralloc.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080033
Jesse Hall99c7dbb2013-03-14 14:29:29 -070034#include "DisplayHardware/DisplaySurface.h"
Mathias Agopian1b031492012-06-20 17:51:20 -070035#include "DisplayHardware/HWComposer.h"
Mathias Agopian875d8e12013-06-07 15:35:48 -070036#include "RenderEngine/RenderEngine.h"
Mathias Agopian1b031492012-06-20 17:51:20 -070037
Mathias Agopianda8d0a52012-09-04 15:05:38 -070038#include "clz.h"
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -070039#include "DisplayDevice.h"
Mathias Agopianc7d14e22011-08-01 16:32:21 -070040#include "SurfaceFlinger.h"
Mathias Agopian13127d82013-03-05 17:47:11 -080041#include "Layer.h"
Mathias Agopian1f7bec62010-06-25 18:02:21 -070042
Mathias Agopiana4912602012-07-12 14:25:33 -070043// ----------------------------------------------------------------------------
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080044using namespace android;
Mathias Agopiana4912602012-07-12 14:25:33 -070045// ----------------------------------------------------------------------------
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080046
Andreas Gampe89fd4f72014-11-13 14:18:56 -080047#ifdef EGL_ANDROID_swap_rectangle
48static constexpr bool kEGLAndroidSwapRectangle = true;
49#else
50static constexpr bool kEGLAndroidSwapRectangle = false;
51#endif
52
53#if !defined(EGL_EGLEXT_PROTOTYPES) || !defined(EGL_ANDROID_swap_rectangle)
54// Dummy implementation in case it is missing.
55inline void eglSetSwapRectangleANDROID (EGLDisplay, EGLSurface, EGLint, EGLint, EGLint, EGLint) {
56}
57#endif
58
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080059/*
60 * Initialize the display to the specified values.
61 *
62 */
63
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -070064DisplayDevice::DisplayDevice(
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080065 const sp<SurfaceFlinger>& flinger,
Jamie Gennisdd3cb842012-10-19 18:19:11 -070066 DisplayType type,
Jesse Hallffe1f192013-03-22 15:13:48 -070067 int32_t hwcId,
Jesse Hall19e87292013-12-23 21:02:15 -080068 int format,
Jamie Gennisdd3cb842012-10-19 18:19:11 -070069 bool isSecure,
70 const wp<IBinder>& displayToken,
Jesse Hall99c7dbb2013-03-14 14:29:29 -070071 const sp<DisplaySurface>& displaySurface,
Mathias Agopiandb89edc2013-08-02 01:40:18 -070072 const sp<IGraphicBufferProducer>& producer,
Mathias Agopiana4912602012-07-12 14:25:33 -070073 EGLConfig config)
Jesse Hallb7a05492014-08-14 15:45:06 -070074 : lastCompositionHadVisibleLayers(false),
75 mFlinger(flinger),
Jesse Hallffe1f192013-03-22 15:13:48 -070076 mType(type), mHwcDisplayId(hwcId),
Chih-Wei Huang27e25622013-01-07 17:33:56 +080077 mDisplayToken(displayToken),
Jesse Hall99c7dbb2013-03-14 14:29:29 -070078 mDisplaySurface(displaySurface),
Mathias Agopian92a979a2012-08-02 18:32:23 -070079 mDisplay(EGL_NO_DISPLAY),
80 mSurface(EGL_NO_SURFACE),
Mathias Agopian92a979a2012-08-02 18:32:23 -070081 mDisplayWidth(), mDisplayHeight(), mFormat(),
82 mFlags(),
83 mPageFlipCount(),
Jamie Gennisdd3cb842012-10-19 18:19:11 -070084 mIsSecure(isSecure),
Jesse Hall01e29052013-02-19 16:13:35 -080085 mLayerStack(NO_LAYER_STACK),
Prashant Malani2c9b11f2014-05-25 01:36:31 -070086 mOrientation(),
Michael Lentine6c9e34a2014-07-14 13:48:55 -070087 mPowerMode(HWC_POWER_MODE_OFF),
88 mActiveConfig(0)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080089{
james.zhang5e8eb5e2015-12-01 17:55:11 +080090 Surface* surface;
91 mNativeWindow = surface = new Surface(producer, false);
Jesse Hallffe1f192013-03-22 15:13:48 -070092 ANativeWindow* const window = mNativeWindow.get();
93
Jesse Hallffe1f192013-03-22 15:13:48 -070094 /*
95 * Create our display's surface
96 */
97
james.zhang5e8eb5e2015-12-01 17:55:11 +080098 EGLSurface eglSurface;
Jesse Hallffe1f192013-03-22 15:13:48 -070099 EGLDisplay display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
Jesse Hall19e87292013-12-23 21:02:15 -0800100 if (config == EGL_NO_CONFIG) {
101 config = RenderEngine::chooseEglConfig(display, format);
102 }
james.zhang5e8eb5e2015-12-01 17:55:11 +0800103 eglSurface = eglCreateWindowSurface(display, config, window, NULL);
104 eglQuerySurface(display, eglSurface, EGL_WIDTH, &mDisplayWidth);
105 eglQuerySurface(display, eglSurface, EGL_HEIGHT, &mDisplayHeight);
Jesse Hallffe1f192013-03-22 15:13:48 -0700106
John Dong4ee56962014-02-21 12:37:59 -0800107 // Make sure that composition can never be stalled by a virtual display
108 // consumer that isn't processing buffers fast enough. We have to do this
109 // in two places:
110 // * Here, in case the display is composed entirely by HWC.
111 // * In makeCurrent(), using eglSwapInterval. Some EGL drivers set the
112 // window's swap interval in eglMakeCurrent, so they'll override the
113 // interval we set here.
114 if (mType >= DisplayDevice::DISPLAY_VIRTUAL)
115 window->setSwapInterval(window, 0);
116
Michael Lentine47e45402014-07-18 15:34:25 -0700117 mConfig = config;
Jesse Hallffe1f192013-03-22 15:13:48 -0700118 mDisplay = display;
james.zhang5e8eb5e2015-12-01 17:55:11 +0800119 mSurface = eglSurface;
Jesse Hallffe1f192013-03-22 15:13:48 -0700120 mFormat = format;
121 mPageFlipCount = 0;
122 mViewport.makeInvalid();
123 mFrame.makeInvalid();
124
125 // virtual displays are always considered enabled
Prashant Malani2c9b11f2014-05-25 01:36:31 -0700126 mPowerMode = (mType >= DisplayDevice::DISPLAY_VIRTUAL) ?
127 HWC_POWER_MODE_NORMAL : HWC_POWER_MODE_OFF;
Jesse Hallffe1f192013-03-22 15:13:48 -0700128
129 // Name the display. The name will be replaced shortly if the display
130 // was created with createDisplay().
131 switch (mType) {
132 case DISPLAY_PRIMARY:
133 mDisplayName = "Built-in Screen";
134 break;
135 case DISPLAY_EXTERNAL:
136 mDisplayName = "HDMI Screen";
137 break;
138 default:
139 mDisplayName = "Virtual Screen"; // e.g. Overlay #n
140 break;
141 }
142
143 // initialize the display orientation transform.
144 setProjection(DisplayState::eOrientationDefault, mViewport, mFrame);
james.zhang5e8eb5e2015-12-01 17:55:11 +0800145
146#ifdef NUM_FRAMEBUFFER_SURFACE_BUFFERS
147 surface->allocateBuffers();
148#endif
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800149}
150
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700151DisplayDevice::~DisplayDevice() {
Mathias Agopian92a979a2012-08-02 18:32:23 -0700152 if (mSurface != EGL_NO_SURFACE) {
153 eglDestroySurface(mDisplay, mSurface);
154 mSurface = EGL_NO_SURFACE;
155 }
156}
157
Jesse Hall02d86562013-03-25 14:43:23 -0700158void DisplayDevice::disconnect(HWComposer& hwc) {
159 if (mHwcDisplayId >= 0) {
160 hwc.disconnectDisplay(mHwcDisplayId);
161 if (mHwcDisplayId >= DISPLAY_VIRTUAL)
162 hwc.freeDisplayId(mHwcDisplayId);
163 mHwcDisplayId = -1;
164 }
165}
166
Mathias Agopian92a979a2012-08-02 18:32:23 -0700167bool DisplayDevice::isValid() const {
168 return mFlinger != NULL;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800169}
170
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700171int DisplayDevice::getWidth() const {
Mathias Agopiana4912602012-07-12 14:25:33 -0700172 return mDisplayWidth;
173}
174
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700175int DisplayDevice::getHeight() const {
Mathias Agopiana4912602012-07-12 14:25:33 -0700176 return mDisplayHeight;
177}
178
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700179PixelFormat DisplayDevice::getFormat() const {
Mathias Agopiana4912602012-07-12 14:25:33 -0700180 return mFormat;
181}
182
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700183EGLSurface DisplayDevice::getEGLSurface() const {
Mathias Agopiana4912602012-07-12 14:25:33 -0700184 return mSurface;
185}
186
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700187void DisplayDevice::setDisplayName(const String8& displayName) {
188 if (!displayName.isEmpty()) {
189 // never override the name with an empty name
190 mDisplayName = displayName;
191 }
192}
193
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700194uint32_t DisplayDevice::getPageFlipCount() const {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700195 return mPageFlipCount;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800196}
197
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700198status_t DisplayDevice::compositionComplete() const {
Jesse Hall99c7dbb2013-03-14 14:29:29 -0700199 return mDisplaySurface->compositionComplete();
Mathias Agopian74faca22009-09-17 16:18:16 -0700200}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800201
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700202void DisplayDevice::flip(const Region& dirty) const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800203{
Mathias Agopian875d8e12013-06-07 15:35:48 -0700204 mFlinger->getRenderEngine().checkErrors();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800205
Andreas Gampe89fd4f72014-11-13 14:18:56 -0800206 if (kEGLAndroidSwapRectangle) {
207 if (mFlags & SWAP_RECTANGLE) {
208 const Region newDirty(dirty.intersect(bounds()));
209 const Rect b(newDirty.getBounds());
210 eglSetSwapRectangleANDROID(mDisplay, mSurface,
211 b.left, b.top, b.width(), b.height());
212 }
Jesse Hall01e29052013-02-19 16:13:35 -0800213 }
Mathias Agopiand8707032012-09-18 01:21:55 -0700214
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700215 mPageFlipCount++;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800216}
217
Dan Stoza71433162014-02-04 16:22:36 -0800218status_t DisplayDevice::beginFrame(bool mustRecompose) const {
219 return mDisplaySurface->beginFrame(mustRecompose);
Jesse Hall028dc8f2013-08-20 16:35:32 -0700220}
221
Jesse Hall38efe862013-04-06 23:12:29 -0700222status_t DisplayDevice::prepareFrame(const HWComposer& hwc) const {
223 DisplaySurface::CompositionType compositionType;
224 bool haveGles = hwc.hasGlesComposition(mHwcDisplayId);
225 bool haveHwc = hwc.hasHwcComposition(mHwcDisplayId);
226 if (haveGles && haveHwc) {
227 compositionType = DisplaySurface::COMPOSITION_MIXED;
228 } else if (haveGles) {
229 compositionType = DisplaySurface::COMPOSITION_GLES;
230 } else if (haveHwc) {
231 compositionType = DisplaySurface::COMPOSITION_HWC;
232 } else {
233 // Nothing to do -- when turning the screen off we get a frame like
234 // this. Call it a HWC frame since we won't be doing any GLES work but
235 // will do a prepare/set cycle.
236 compositionType = DisplaySurface::COMPOSITION_HWC;
237 }
238 return mDisplaySurface->prepareFrame(compositionType);
239}
240
Mathias Agopianda27af92012-09-13 18:17:13 -0700241void DisplayDevice::swapBuffers(HWComposer& hwc) const {
Andy McFadden22a99f02013-08-20 16:04:47 -0700242 // We need to call eglSwapBuffers() if:
243 // (1) we don't have a hardware composer, or
244 // (2) we did GLES composition this frame, and either
245 // (a) we have framebuffer target support (not present on legacy
246 // devices, where HWComposer::commit() handles things); or
247 // (b) this is a virtual display
Jesse Hall99c7dbb2013-03-14 14:29:29 -0700248 if (hwc.initCheck() != NO_ERROR ||
249 (hwc.hasGlesComposition(mHwcDisplayId) &&
Andy McFadden22a99f02013-08-20 16:04:47 -0700250 (hwc.supportsFramebufferTarget() || mType >= DISPLAY_VIRTUAL))) {
Jesse Hall99c7dbb2013-03-14 14:29:29 -0700251 EGLBoolean success = eglSwapBuffers(mDisplay, mSurface);
252 if (!success) {
253 EGLint error = eglGetError();
254 if (error == EGL_CONTEXT_LOST ||
255 mType == DisplayDevice::DISPLAY_PRIMARY) {
256 LOG_ALWAYS_FATAL("eglSwapBuffers(%p, %p) failed with 0x%08x",
257 mDisplay, mSurface, error);
258 } else {
259 ALOGE("eglSwapBuffers(%p, %p) failed with 0x%08x",
260 mDisplay, mSurface, error);
Mathias Agopianda27af92012-09-13 18:17:13 -0700261 }
262 }
263 }
Mathias Agopian52e21482012-09-24 18:07:21 -0700264
Jesse Hall99c7dbb2013-03-14 14:29:29 -0700265 status_t result = mDisplaySurface->advanceFrame();
266 if (result != NO_ERROR) {
267 ALOGE("[%s] failed pushing new frame to HWC: %d",
268 mDisplayName.string(), result);
Mathias Agopian32341382012-09-25 19:16:28 -0700269 }
Mathias Agopianda27af92012-09-13 18:17:13 -0700270}
271
272void DisplayDevice::onSwapBuffersCompleted(HWComposer& hwc) const {
273 if (hwc.initCheck() == NO_ERROR) {
Jesse Hall851cfe82013-03-20 13:44:00 -0700274 mDisplaySurface->onFrameCommitted();
Mathias Agopianda27af92012-09-13 18:17:13 -0700275 }
276}
277
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700278uint32_t DisplayDevice::getFlags() const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800279{
280 return mFlags;
281}
282
Mathias Agopian875d8e12013-06-07 15:35:48 -0700283EGLBoolean DisplayDevice::makeCurrent(EGLDisplay dpy, EGLContext ctx) const {
Mathias Agopianda8d0a52012-09-04 15:05:38 -0700284 EGLBoolean result = EGL_TRUE;
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700285 EGLSurface sur = eglGetCurrentSurface(EGL_DRAW);
Mathias Agopian875d8e12013-06-07 15:35:48 -0700286 if (sur != mSurface) {
287 result = eglMakeCurrent(dpy, mSurface, mSurface, ctx);
Mathias Agopianda8d0a52012-09-04 15:05:38 -0700288 if (result == EGL_TRUE) {
Jesse Hallf460f552013-08-06 17:08:53 -0700289 if (mType >= DisplayDevice::DISPLAY_VIRTUAL)
290 eglSwapInterval(dpy, 0);
Mathias Agopianda8d0a52012-09-04 15:05:38 -0700291 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700292 }
Mathias Agopian931bda12013-08-28 18:11:46 -0700293 setViewportAndProjection();
Mathias Agopianda8d0a52012-09-04 15:05:38 -0700294 return result;
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700295}
296
Mathias Agopian875d8e12013-06-07 15:35:48 -0700297void DisplayDevice::setViewportAndProjection() const {
298 size_t w = mDisplayWidth;
299 size_t h = mDisplayHeight;
Dan Stozac1879002014-05-22 15:59:05 -0700300 Rect sourceCrop(0, 0, w, h);
Riley Andrewsc3ebe662014-09-04 16:20:31 -0700301 mFlinger->getRenderEngine().setViewportAndProjection(w, h, sourceCrop, h,
302 false, Transform::ROT_0);
Mathias Agopianbae92d02012-09-28 01:00:47 -0700303}
304
Mathias Agopian1b031492012-06-20 17:51:20 -0700305// ----------------------------------------------------------------------------
306
Mathias Agopian13127d82013-03-05 17:47:11 -0800307void DisplayDevice::setVisibleLayersSortedByZ(const Vector< sp<Layer> >& layers) {
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700308 mVisibleLayersSortedByZ = layers;
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700309}
310
Mathias Agopian13127d82013-03-05 17:47:11 -0800311const Vector< sp<Layer> >& DisplayDevice::getVisibleLayersSortedByZ() const {
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700312 return mVisibleLayersSortedByZ;
313}
314
Mathias Agopiancd60f992012-08-16 16:28:27 -0700315Region DisplayDevice::getDirtyRegion(bool repaintEverything) const {
316 Region dirty;
Mathias Agopiancd60f992012-08-16 16:28:27 -0700317 if (repaintEverything) {
318 dirty.set(getBounds());
319 } else {
Mathias Agopianda8d0a52012-09-04 15:05:38 -0700320 const Transform& planeTransform(mGlobalTransform);
Mathias Agopiancd60f992012-08-16 16:28:27 -0700321 dirty = planeTransform.transform(this->dirtyRegion);
322 dirty.andSelf(getBounds());
323 }
324 return dirty;
325}
326
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700327// ----------------------------------------------------------------------------
Prashant Malani2c9b11f2014-05-25 01:36:31 -0700328void DisplayDevice::setPowerMode(int mode) {
329 mPowerMode = mode;
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700330}
331
Prashant Malani2c9b11f2014-05-25 01:36:31 -0700332int DisplayDevice::getPowerMode() const {
333 return mPowerMode;
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700334}
335
Prashant Malani2c9b11f2014-05-25 01:36:31 -0700336bool DisplayDevice::isDisplayOn() const {
337 return (mPowerMode != HWC_POWER_MODE_OFF);
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700338}
339
340// ----------------------------------------------------------------------------
Michael Lentine6c9e34a2014-07-14 13:48:55 -0700341void DisplayDevice::setActiveConfig(int mode) {
342 mActiveConfig = mode;
343}
344
345int DisplayDevice::getActiveConfig() const {
346 return mActiveConfig;
347}
348
349// ----------------------------------------------------------------------------
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700350
Mathias Agopian28947d72012-08-08 18:51:15 -0700351void DisplayDevice::setLayerStack(uint32_t stack) {
352 mLayerStack = stack;
353 dirtyRegion.set(bounds());
354}
355
356// ----------------------------------------------------------------------------
357
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700358uint32_t DisplayDevice::getOrientationTransform() const {
359 uint32_t transform = 0;
360 switch (mOrientation) {
361 case DisplayState::eOrientationDefault:
362 transform = Transform::ROT_0;
363 break;
364 case DisplayState::eOrientation90:
365 transform = Transform::ROT_90;
366 break;
367 case DisplayState::eOrientation180:
368 transform = Transform::ROT_180;
369 break;
370 case DisplayState::eOrientation270:
371 transform = Transform::ROT_270;
372 break;
373 }
374 return transform;
375}
376
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700377status_t DisplayDevice::orientationToTransfrom(
Mathias Agopian1b031492012-06-20 17:51:20 -0700378 int orientation, int w, int h, Transform* tr)
379{
380 uint32_t flags = 0;
381 switch (orientation) {
Mathias Agopian3165cc22012-08-08 19:42:09 -0700382 case DisplayState::eOrientationDefault:
Mathias Agopian1b031492012-06-20 17:51:20 -0700383 flags = Transform::ROT_0;
384 break;
Mathias Agopian3165cc22012-08-08 19:42:09 -0700385 case DisplayState::eOrientation90:
Mathias Agopian1b031492012-06-20 17:51:20 -0700386 flags = Transform::ROT_90;
387 break;
Mathias Agopian3165cc22012-08-08 19:42:09 -0700388 case DisplayState::eOrientation180:
Mathias Agopian1b031492012-06-20 17:51:20 -0700389 flags = Transform::ROT_180;
390 break;
Mathias Agopian3165cc22012-08-08 19:42:09 -0700391 case DisplayState::eOrientation270:
Mathias Agopian1b031492012-06-20 17:51:20 -0700392 flags = Transform::ROT_270;
393 break;
394 default:
395 return BAD_VALUE;
396 }
397 tr->set(flags, w, h);
398 return NO_ERROR;
399}
400
Michael Lentine47e45402014-07-18 15:34:25 -0700401void DisplayDevice::setDisplaySize(const int newWidth, const int newHeight) {
402 dirtyRegion.set(getBounds());
403
Michael Lentinef2568de2014-08-20 10:51:23 -0700404 if (mSurface != EGL_NO_SURFACE) {
405 eglDestroySurface(mDisplay, mSurface);
406 mSurface = EGL_NO_SURFACE;
407 }
408
Michael Lentine47e45402014-07-18 15:34:25 -0700409 mDisplaySurface->resizeBuffers(newWidth, newHeight);
410
411 ANativeWindow* const window = mNativeWindow.get();
412 mSurface = eglCreateWindowSurface(mDisplay, mConfig, window, NULL);
413 eglQuerySurface(mDisplay, mSurface, EGL_WIDTH, &mDisplayWidth);
414 eglQuerySurface(mDisplay, mSurface, EGL_HEIGHT, &mDisplayHeight);
415
416 LOG_FATAL_IF(mDisplayWidth != newWidth,
417 "Unable to set new width to %d", newWidth);
418 LOG_FATAL_IF(mDisplayHeight != newHeight,
419 "Unable to set new height to %d", newHeight);
420}
421
Mathias Agopian00e8c7a2012-09-04 19:30:46 -0700422void DisplayDevice::setProjection(int orientation,
Mathias Agopianf5f714a2013-02-26 16:54:05 -0800423 const Rect& newViewport, const Rect& newFrame) {
424 Rect viewport(newViewport);
425 Rect frame(newFrame);
426
427 const int w = mDisplayWidth;
428 const int h = mDisplayHeight;
429
430 Transform R;
431 DisplayDevice::orientationToTransfrom(orientation, w, h, &R);
432
433 if (!frame.isValid()) {
434 // the destination frame can be invalid if it has never been set,
435 // in that case we assume the whole display frame.
436 frame = Rect(w, h);
437 }
438
439 if (viewport.isEmpty()) {
440 // viewport can be invalid if it has never been set, in that case
441 // we assume the whole display size.
442 // it's also invalid to have an empty viewport, so we handle that
443 // case in the same way.
444 viewport = Rect(w, h);
445 if (R.getOrientation() & Transform::ROT_90) {
446 // viewport is always specified in the logical orientation
447 // of the display (ie: post-rotation).
448 swap(viewport.right, viewport.bottom);
449 }
450 }
451
452 dirtyRegion.set(getBounds());
453
454 Transform TL, TP, S;
455 float src_width = viewport.width();
456 float src_height = viewport.height();
457 float dst_width = frame.width();
458 float dst_height = frame.height();
459 if (src_width != dst_width || src_height != dst_height) {
460 float sx = dst_width / src_width;
461 float sy = dst_height / src_height;
462 S.set(sx, 0, 0, sy);
463 }
464
465 float src_x = viewport.left;
466 float src_y = viewport.top;
467 float dst_x = frame.left;
468 float dst_y = frame.top;
469 TL.set(-src_x, -src_y);
470 TP.set(dst_x, dst_y);
471
472 // The viewport and frame are both in the logical orientation.
473 // Apply the logical translation, scale to physical size, apply the
474 // physical translation and finally rotate to the physical orientation.
475 mGlobalTransform = R * TP * S * TL;
476
477 const uint8_t type = mGlobalTransform.getType();
478 mNeedsFiltering = (!mGlobalTransform.preserveRects() ||
479 (type >= Transform::SCALE));
480
481 mScissor = mGlobalTransform.transform(viewport);
482 if (mScissor.isEmpty()) {
Mathias Agopian6c7f25a2013-05-09 20:37:10 -0700483 mScissor = getBounds();
Mathias Agopianf5f714a2013-02-26 16:54:05 -0800484 }
485
Mathias Agopianda8d0a52012-09-04 15:05:38 -0700486 mOrientation = orientation;
Mathias Agopian00e8c7a2012-09-04 19:30:46 -0700487 mViewport = viewport;
488 mFrame = frame;
Mathias Agopian1b031492012-06-20 17:51:20 -0700489}
Mathias Agopian1d12d8a2012-09-18 01:38:00 -0700490
Mathias Agopian74d211a2013-04-22 16:55:35 +0200491void DisplayDevice::dump(String8& result) const {
Mathias Agopian1d12d8a2012-09-18 01:38:00 -0700492 const Transform& tr(mGlobalTransform);
Mathias Agopian74d211a2013-04-22 16:55:35 +0200493 result.appendFormat(
Mathias Agopian1d12d8a2012-09-18 01:38:00 -0700494 "+ DisplayDevice: %s\n"
Jesse Hall02d86562013-03-25 14:43:23 -0700495 " type=%x, hwcId=%d, layerStack=%u, (%4dx%4d), ANativeWindow=%p, orient=%2d (type=%08x), "
Pablo Ceballosb5b35632016-02-23 11:18:51 -0800496 "flips=%u, isSecure=%d, powerMode=%d, activeConfig=%d, numLayers=%zu\n"
Mathias Agopian766dc492012-10-30 18:08:06 -0700497 " v:[%d,%d,%d,%d], f:[%d,%d,%d,%d], s:[%d,%d,%d,%d],"
Mathias Agopian1d12d8a2012-09-18 01:38:00 -0700498 "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 -0700499 mDisplayName.string(), mType, mHwcDisplayId,
Mathias Agopian1d12d8a2012-09-18 01:38:00 -0700500 mLayerStack, mDisplayWidth, mDisplayHeight, mNativeWindow.get(),
501 mOrientation, tr.getType(), getPageFlipCount(),
Pablo Ceballosb5b35632016-02-23 11:18:51 -0800502 mIsSecure, mPowerMode, mActiveConfig,
Michael Lentine6c9e34a2014-07-14 13:48:55 -0700503 mVisibleLayersSortedByZ.size(),
Mathias Agopian1d12d8a2012-09-18 01:38:00 -0700504 mViewport.left, mViewport.top, mViewport.right, mViewport.bottom,
505 mFrame.left, mFrame.top, mFrame.right, mFrame.bottom,
Mathias Agopian766dc492012-10-30 18:08:06 -0700506 mScissor.left, mScissor.top, mScissor.right, mScissor.bottom,
Mathias Agopian1d12d8a2012-09-18 01:38:00 -0700507 tr[0][0], tr[1][0], tr[2][0],
508 tr[0][1], tr[1][1], tr[2][1],
509 tr[0][2], tr[1][2], tr[2][2]);
510
Jesse Hall99c7dbb2013-03-14 14:29:29 -0700511 String8 surfaceDump;
Dan Stozaf10c46e2014-11-11 10:32:31 -0800512 mDisplaySurface->dumpAsString(surfaceDump);
Jesse Hall99c7dbb2013-03-14 14:29:29 -0700513 result.append(surfaceDump);
Mathias Agopian1d12d8a2012-09-18 01:38:00 -0700514}