blob: bdf8f7404af0d7ae1446c3b068e94076aa198ffc [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),
Mathias Agopian92a979a2012-08-02 18:32:23 -070085 mSecureLayerVisible(false),
Jesse Hall01e29052013-02-19 16:13:35 -080086 mLayerStack(NO_LAYER_STACK),
Prashant Malani2c9b11f2014-05-25 01:36:31 -070087 mOrientation(),
Michael Lentine6c9e34a2014-07-14 13:48:55 -070088 mPowerMode(HWC_POWER_MODE_OFF),
89 mActiveConfig(0)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080090{
james.zhang5e8eb5e2015-12-01 17:55:11 +080091 Surface* surface;
92 mNativeWindow = surface = new Surface(producer, false);
Jesse Hallffe1f192013-03-22 15:13:48 -070093 ANativeWindow* const window = mNativeWindow.get();
94
Jesse Hallffe1f192013-03-22 15:13:48 -070095 /*
96 * Create our display's surface
97 */
98
james.zhang5e8eb5e2015-12-01 17:55:11 +080099 EGLSurface eglSurface;
Jesse Hallffe1f192013-03-22 15:13:48 -0700100 EGLDisplay display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
Jesse Hall19e87292013-12-23 21:02:15 -0800101 if (config == EGL_NO_CONFIG) {
102 config = RenderEngine::chooseEglConfig(display, format);
103 }
james.zhang5e8eb5e2015-12-01 17:55:11 +0800104 eglSurface = eglCreateWindowSurface(display, config, window, NULL);
105 eglQuerySurface(display, eglSurface, EGL_WIDTH, &mDisplayWidth);
106 eglQuerySurface(display, eglSurface, EGL_HEIGHT, &mDisplayHeight);
Jesse Hallffe1f192013-03-22 15:13:48 -0700107
John Dong4ee56962014-02-21 12:37:59 -0800108 // Make sure that composition can never be stalled by a virtual display
109 // consumer that isn't processing buffers fast enough. We have to do this
110 // in two places:
111 // * Here, in case the display is composed entirely by HWC.
112 // * In makeCurrent(), using eglSwapInterval. Some EGL drivers set the
113 // window's swap interval in eglMakeCurrent, so they'll override the
114 // interval we set here.
115 if (mType >= DisplayDevice::DISPLAY_VIRTUAL)
116 window->setSwapInterval(window, 0);
117
Michael Lentine47e45402014-07-18 15:34:25 -0700118 mConfig = config;
Jesse Hallffe1f192013-03-22 15:13:48 -0700119 mDisplay = display;
james.zhang5e8eb5e2015-12-01 17:55:11 +0800120 mSurface = eglSurface;
Jesse Hallffe1f192013-03-22 15:13:48 -0700121 mFormat = format;
122 mPageFlipCount = 0;
123 mViewport.makeInvalid();
124 mFrame.makeInvalid();
125
126 // virtual displays are always considered enabled
Prashant Malani2c9b11f2014-05-25 01:36:31 -0700127 mPowerMode = (mType >= DisplayDevice::DISPLAY_VIRTUAL) ?
128 HWC_POWER_MODE_NORMAL : HWC_POWER_MODE_OFF;
Jesse Hallffe1f192013-03-22 15:13:48 -0700129
130 // Name the display. The name will be replaced shortly if the display
131 // was created with createDisplay().
132 switch (mType) {
133 case DISPLAY_PRIMARY:
134 mDisplayName = "Built-in Screen";
135 break;
136 case DISPLAY_EXTERNAL:
137 mDisplayName = "HDMI Screen";
138 break;
139 default:
140 mDisplayName = "Virtual Screen"; // e.g. Overlay #n
141 break;
142 }
143
144 // initialize the display orientation transform.
145 setProjection(DisplayState::eOrientationDefault, mViewport, mFrame);
james.zhang5e8eb5e2015-12-01 17:55:11 +0800146
147#ifdef NUM_FRAMEBUFFER_SURFACE_BUFFERS
148 surface->allocateBuffers();
149#endif
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800150}
151
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700152DisplayDevice::~DisplayDevice() {
Mathias Agopian92a979a2012-08-02 18:32:23 -0700153 if (mSurface != EGL_NO_SURFACE) {
154 eglDestroySurface(mDisplay, mSurface);
155 mSurface = EGL_NO_SURFACE;
156 }
157}
158
Jesse Hall02d86562013-03-25 14:43:23 -0700159void DisplayDevice::disconnect(HWComposer& hwc) {
160 if (mHwcDisplayId >= 0) {
161 hwc.disconnectDisplay(mHwcDisplayId);
162 if (mHwcDisplayId >= DISPLAY_VIRTUAL)
163 hwc.freeDisplayId(mHwcDisplayId);
164 mHwcDisplayId = -1;
165 }
166}
167
Mathias Agopian92a979a2012-08-02 18:32:23 -0700168bool DisplayDevice::isValid() const {
169 return mFlinger != NULL;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800170}
171
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700172int DisplayDevice::getWidth() const {
Mathias Agopiana4912602012-07-12 14:25:33 -0700173 return mDisplayWidth;
174}
175
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700176int DisplayDevice::getHeight() const {
Mathias Agopiana4912602012-07-12 14:25:33 -0700177 return mDisplayHeight;
178}
179
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700180PixelFormat DisplayDevice::getFormat() const {
Mathias Agopiana4912602012-07-12 14:25:33 -0700181 return mFormat;
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 -0700199status_t DisplayDevice::compositionComplete() const {
Jesse Hall99c7dbb2013-03-14 14:29:29 -0700200 return mDisplaySurface->compositionComplete();
Mathias Agopian74faca22009-09-17 16:18:16 -0700201}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800202
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700203void DisplayDevice::flip(const Region& dirty) const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800204{
Mathias Agopian875d8e12013-06-07 15:35:48 -0700205 mFlinger->getRenderEngine().checkErrors();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800206
Andreas Gampe89fd4f72014-11-13 14:18:56 -0800207 if (kEGLAndroidSwapRectangle) {
208 if (mFlags & SWAP_RECTANGLE) {
209 const Region newDirty(dirty.intersect(bounds()));
210 const Rect b(newDirty.getBounds());
211 eglSetSwapRectangleANDROID(mDisplay, mSurface,
212 b.left, b.top, b.width(), b.height());
213 }
Jesse Hall01e29052013-02-19 16:13:35 -0800214 }
Mathias Agopiand8707032012-09-18 01:21:55 -0700215
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700216 mPageFlipCount++;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800217}
218
Dan Stoza71433162014-02-04 16:22:36 -0800219status_t DisplayDevice::beginFrame(bool mustRecompose) const {
220 return mDisplaySurface->beginFrame(mustRecompose);
Jesse Hall028dc8f2013-08-20 16:35:32 -0700221}
222
Jesse Hall38efe862013-04-06 23:12:29 -0700223status_t DisplayDevice::prepareFrame(const HWComposer& hwc) const {
224 DisplaySurface::CompositionType compositionType;
225 bool haveGles = hwc.hasGlesComposition(mHwcDisplayId);
226 bool haveHwc = hwc.hasHwcComposition(mHwcDisplayId);
227 if (haveGles && haveHwc) {
228 compositionType = DisplaySurface::COMPOSITION_MIXED;
229 } else if (haveGles) {
230 compositionType = DisplaySurface::COMPOSITION_GLES;
231 } else if (haveHwc) {
232 compositionType = DisplaySurface::COMPOSITION_HWC;
233 } else {
234 // Nothing to do -- when turning the screen off we get a frame like
235 // this. Call it a HWC frame since we won't be doing any GLES work but
236 // will do a prepare/set cycle.
237 compositionType = DisplaySurface::COMPOSITION_HWC;
238 }
239 return mDisplaySurface->prepareFrame(compositionType);
240}
241
Mathias Agopianda27af92012-09-13 18:17:13 -0700242void DisplayDevice::swapBuffers(HWComposer& hwc) const {
Andy McFadden22a99f02013-08-20 16:04:47 -0700243 // We need to call eglSwapBuffers() if:
244 // (1) we don't have a hardware composer, or
245 // (2) we did GLES composition this frame, and either
246 // (a) we have framebuffer target support (not present on legacy
247 // devices, where HWComposer::commit() handles things); or
248 // (b) this is a virtual display
Jesse Hall99c7dbb2013-03-14 14:29:29 -0700249 if (hwc.initCheck() != NO_ERROR ||
250 (hwc.hasGlesComposition(mHwcDisplayId) &&
Andy McFadden22a99f02013-08-20 16:04:47 -0700251 (hwc.supportsFramebufferTarget() || mType >= DISPLAY_VIRTUAL))) {
Jesse Hall99c7dbb2013-03-14 14:29:29 -0700252 EGLBoolean success = eglSwapBuffers(mDisplay, mSurface);
253 if (!success) {
254 EGLint error = eglGetError();
255 if (error == EGL_CONTEXT_LOST ||
256 mType == DisplayDevice::DISPLAY_PRIMARY) {
257 LOG_ALWAYS_FATAL("eglSwapBuffers(%p, %p) failed with 0x%08x",
258 mDisplay, mSurface, error);
259 } else {
260 ALOGE("eglSwapBuffers(%p, %p) failed with 0x%08x",
261 mDisplay, mSurface, error);
Mathias Agopianda27af92012-09-13 18:17:13 -0700262 }
263 }
264 }
Mathias Agopian52e21482012-09-24 18:07:21 -0700265
Jesse Hall99c7dbb2013-03-14 14:29:29 -0700266 status_t result = mDisplaySurface->advanceFrame();
267 if (result != NO_ERROR) {
268 ALOGE("[%s] failed pushing new frame to HWC: %d",
269 mDisplayName.string(), result);
Mathias Agopian32341382012-09-25 19:16:28 -0700270 }
Mathias Agopianda27af92012-09-13 18:17:13 -0700271}
272
273void DisplayDevice::onSwapBuffersCompleted(HWComposer& hwc) const {
274 if (hwc.initCheck() == NO_ERROR) {
Jesse Hall851cfe82013-03-20 13:44:00 -0700275 mDisplaySurface->onFrameCommitted();
Mathias Agopianda27af92012-09-13 18:17:13 -0700276 }
277}
278
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700279uint32_t DisplayDevice::getFlags() const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800280{
281 return mFlags;
282}
283
Mathias Agopian875d8e12013-06-07 15:35:48 -0700284EGLBoolean DisplayDevice::makeCurrent(EGLDisplay dpy, EGLContext ctx) const {
Mathias Agopianda8d0a52012-09-04 15:05:38 -0700285 EGLBoolean result = EGL_TRUE;
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700286 EGLSurface sur = eglGetCurrentSurface(EGL_DRAW);
Mathias Agopian875d8e12013-06-07 15:35:48 -0700287 if (sur != mSurface) {
288 result = eglMakeCurrent(dpy, mSurface, mSurface, ctx);
Mathias Agopianda8d0a52012-09-04 15:05:38 -0700289 if (result == EGL_TRUE) {
Jesse Hallf460f552013-08-06 17:08:53 -0700290 if (mType >= DisplayDevice::DISPLAY_VIRTUAL)
291 eglSwapInterval(dpy, 0);
Mathias Agopianda8d0a52012-09-04 15:05:38 -0700292 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700293 }
Mathias Agopian931bda12013-08-28 18:11:46 -0700294 setViewportAndProjection();
Mathias Agopianda8d0a52012-09-04 15:05:38 -0700295 return result;
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700296}
297
Mathias Agopian875d8e12013-06-07 15:35:48 -0700298void DisplayDevice::setViewportAndProjection() const {
299 size_t w = mDisplayWidth;
300 size_t h = mDisplayHeight;
Dan Stozac1879002014-05-22 15:59:05 -0700301 Rect sourceCrop(0, 0, w, h);
Riley Andrewsc3ebe662014-09-04 16:20:31 -0700302 mFlinger->getRenderEngine().setViewportAndProjection(w, h, sourceCrop, h,
303 false, Transform::ROT_0);
Mathias Agopianbae92d02012-09-28 01:00:47 -0700304}
305
Mathias Agopian1b031492012-06-20 17:51:20 -0700306// ----------------------------------------------------------------------------
307
Mathias Agopian13127d82013-03-05 17:47:11 -0800308void DisplayDevice::setVisibleLayersSortedByZ(const Vector< sp<Layer> >& layers) {
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700309 mVisibleLayersSortedByZ = layers;
Mathias Agopianef7b9c72012-08-10 15:22:19 -0700310 mSecureLayerVisible = false;
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700311 size_t count = layers.size();
312 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -0800313 const sp<Layer>& layer(layers[i]);
Mathias Agopianf5f714a2013-02-26 16:54:05 -0800314 if (layer->isSecure()) {
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700315 mSecureLayerVisible = true;
316 }
317 }
318}
319
Mathias Agopian13127d82013-03-05 17:47:11 -0800320const Vector< sp<Layer> >& DisplayDevice::getVisibleLayersSortedByZ() const {
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700321 return mVisibleLayersSortedByZ;
322}
323
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700324bool DisplayDevice::getSecureLayerVisible() const {
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700325 return mSecureLayerVisible;
326}
327
Mathias Agopiancd60f992012-08-16 16:28:27 -0700328Region DisplayDevice::getDirtyRegion(bool repaintEverything) const {
329 Region dirty;
Mathias Agopiancd60f992012-08-16 16:28:27 -0700330 if (repaintEverything) {
331 dirty.set(getBounds());
332 } else {
Mathias Agopianda8d0a52012-09-04 15:05:38 -0700333 const Transform& planeTransform(mGlobalTransform);
Mathias Agopiancd60f992012-08-16 16:28:27 -0700334 dirty = planeTransform.transform(this->dirtyRegion);
335 dirty.andSelf(getBounds());
336 }
337 return dirty;
338}
339
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700340// ----------------------------------------------------------------------------
Prashant Malani2c9b11f2014-05-25 01:36:31 -0700341void DisplayDevice::setPowerMode(int mode) {
342 mPowerMode = mode;
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700343}
344
Prashant Malani2c9b11f2014-05-25 01:36:31 -0700345int DisplayDevice::getPowerMode() const {
346 return mPowerMode;
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700347}
348
Prashant Malani2c9b11f2014-05-25 01:36:31 -0700349bool DisplayDevice::isDisplayOn() const {
350 return (mPowerMode != HWC_POWER_MODE_OFF);
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700351}
352
353// ----------------------------------------------------------------------------
Michael Lentine6c9e34a2014-07-14 13:48:55 -0700354void DisplayDevice::setActiveConfig(int mode) {
355 mActiveConfig = mode;
356}
357
358int DisplayDevice::getActiveConfig() const {
359 return mActiveConfig;
360}
361
362// ----------------------------------------------------------------------------
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700363
Mathias Agopian28947d72012-08-08 18:51:15 -0700364void DisplayDevice::setLayerStack(uint32_t stack) {
365 mLayerStack = stack;
366 dirtyRegion.set(bounds());
367}
368
369// ----------------------------------------------------------------------------
370
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700371uint32_t DisplayDevice::getOrientationTransform() const {
372 uint32_t transform = 0;
373 switch (mOrientation) {
374 case DisplayState::eOrientationDefault:
375 transform = Transform::ROT_0;
376 break;
377 case DisplayState::eOrientation90:
378 transform = Transform::ROT_90;
379 break;
380 case DisplayState::eOrientation180:
381 transform = Transform::ROT_180;
382 break;
383 case DisplayState::eOrientation270:
384 transform = Transform::ROT_270;
385 break;
386 }
387 return transform;
388}
389
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700390status_t DisplayDevice::orientationToTransfrom(
Mathias Agopian1b031492012-06-20 17:51:20 -0700391 int orientation, int w, int h, Transform* tr)
392{
393 uint32_t flags = 0;
394 switch (orientation) {
Mathias Agopian3165cc22012-08-08 19:42:09 -0700395 case DisplayState::eOrientationDefault:
Mathias Agopian1b031492012-06-20 17:51:20 -0700396 flags = Transform::ROT_0;
397 break;
Mathias Agopian3165cc22012-08-08 19:42:09 -0700398 case DisplayState::eOrientation90:
Mathias Agopian1b031492012-06-20 17:51:20 -0700399 flags = Transform::ROT_90;
400 break;
Mathias Agopian3165cc22012-08-08 19:42:09 -0700401 case DisplayState::eOrientation180:
Mathias Agopian1b031492012-06-20 17:51:20 -0700402 flags = Transform::ROT_180;
403 break;
Mathias Agopian3165cc22012-08-08 19:42:09 -0700404 case DisplayState::eOrientation270:
Mathias Agopian1b031492012-06-20 17:51:20 -0700405 flags = Transform::ROT_270;
406 break;
407 default:
408 return BAD_VALUE;
409 }
410 tr->set(flags, w, h);
411 return NO_ERROR;
412}
413
Michael Lentine47e45402014-07-18 15:34:25 -0700414void DisplayDevice::setDisplaySize(const int newWidth, const int newHeight) {
415 dirtyRegion.set(getBounds());
416
Michael Lentinef2568de2014-08-20 10:51:23 -0700417 if (mSurface != EGL_NO_SURFACE) {
418 eglDestroySurface(mDisplay, mSurface);
419 mSurface = EGL_NO_SURFACE;
420 }
421
Michael Lentine47e45402014-07-18 15:34:25 -0700422 mDisplaySurface->resizeBuffers(newWidth, newHeight);
423
424 ANativeWindow* const window = mNativeWindow.get();
425 mSurface = eglCreateWindowSurface(mDisplay, mConfig, window, NULL);
426 eglQuerySurface(mDisplay, mSurface, EGL_WIDTH, &mDisplayWidth);
427 eglQuerySurface(mDisplay, mSurface, EGL_HEIGHT, &mDisplayHeight);
428
429 LOG_FATAL_IF(mDisplayWidth != newWidth,
430 "Unable to set new width to %d", newWidth);
431 LOG_FATAL_IF(mDisplayHeight != newHeight,
432 "Unable to set new height to %d", newHeight);
433}
434
Mathias Agopian00e8c7a2012-09-04 19:30:46 -0700435void DisplayDevice::setProjection(int orientation,
Mathias Agopianf5f714a2013-02-26 16:54:05 -0800436 const Rect& newViewport, const Rect& newFrame) {
437 Rect viewport(newViewport);
438 Rect frame(newFrame);
439
440 const int w = mDisplayWidth;
441 const int h = mDisplayHeight;
442
443 Transform R;
444 DisplayDevice::orientationToTransfrom(orientation, w, h, &R);
445
446 if (!frame.isValid()) {
447 // the destination frame can be invalid if it has never been set,
448 // in that case we assume the whole display frame.
449 frame = Rect(w, h);
450 }
451
452 if (viewport.isEmpty()) {
453 // viewport can be invalid if it has never been set, in that case
454 // we assume the whole display size.
455 // it's also invalid to have an empty viewport, so we handle that
456 // case in the same way.
457 viewport = Rect(w, h);
458 if (R.getOrientation() & Transform::ROT_90) {
459 // viewport is always specified in the logical orientation
460 // of the display (ie: post-rotation).
461 swap(viewport.right, viewport.bottom);
462 }
463 }
464
465 dirtyRegion.set(getBounds());
466
467 Transform TL, TP, S;
468 float src_width = viewport.width();
469 float src_height = viewport.height();
470 float dst_width = frame.width();
471 float dst_height = frame.height();
472 if (src_width != dst_width || src_height != dst_height) {
473 float sx = dst_width / src_width;
474 float sy = dst_height / src_height;
475 S.set(sx, 0, 0, sy);
476 }
477
478 float src_x = viewport.left;
479 float src_y = viewport.top;
480 float dst_x = frame.left;
481 float dst_y = frame.top;
482 TL.set(-src_x, -src_y);
483 TP.set(dst_x, dst_y);
484
485 // The viewport and frame are both in the logical orientation.
486 // Apply the logical translation, scale to physical size, apply the
487 // physical translation and finally rotate to the physical orientation.
488 mGlobalTransform = R * TP * S * TL;
489
490 const uint8_t type = mGlobalTransform.getType();
491 mNeedsFiltering = (!mGlobalTransform.preserveRects() ||
492 (type >= Transform::SCALE));
493
494 mScissor = mGlobalTransform.transform(viewport);
495 if (mScissor.isEmpty()) {
Mathias Agopian6c7f25a2013-05-09 20:37:10 -0700496 mScissor = getBounds();
Mathias Agopianf5f714a2013-02-26 16:54:05 -0800497 }
498
Mathias Agopianda8d0a52012-09-04 15:05:38 -0700499 mOrientation = orientation;
Mathias Agopian00e8c7a2012-09-04 19:30:46 -0700500 mViewport = viewport;
501 mFrame = frame;
Mathias Agopian1b031492012-06-20 17:51:20 -0700502}
Mathias Agopian1d12d8a2012-09-18 01:38:00 -0700503
Mathias Agopian74d211a2013-04-22 16:55:35 +0200504void DisplayDevice::dump(String8& result) const {
Mathias Agopian1d12d8a2012-09-18 01:38:00 -0700505 const Transform& tr(mGlobalTransform);
Mathias Agopian74d211a2013-04-22 16:55:35 +0200506 result.appendFormat(
Mathias Agopian1d12d8a2012-09-18 01:38:00 -0700507 "+ DisplayDevice: %s\n"
Jesse Hall02d86562013-03-25 14:43:23 -0700508 " type=%x, hwcId=%d, layerStack=%u, (%4dx%4d), ANativeWindow=%p, orient=%2d (type=%08x), "
Michael Lentine6c9e34a2014-07-14 13:48:55 -0700509 "flips=%u, isSecure=%d, secureVis=%d, powerMode=%d, activeConfig=%d, numLayers=%zu\n"
Mathias Agopian766dc492012-10-30 18:08:06 -0700510 " v:[%d,%d,%d,%d], f:[%d,%d,%d,%d], s:[%d,%d,%d,%d],"
Mathias Agopian1d12d8a2012-09-18 01:38:00 -0700511 "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 -0700512 mDisplayName.string(), mType, mHwcDisplayId,
Mathias Agopian1d12d8a2012-09-18 01:38:00 -0700513 mLayerStack, mDisplayWidth, mDisplayHeight, mNativeWindow.get(),
514 mOrientation, tr.getType(), getPageFlipCount(),
Michael Lentine6c9e34a2014-07-14 13:48:55 -0700515 mIsSecure, mSecureLayerVisible, mPowerMode, mActiveConfig,
516 mVisibleLayersSortedByZ.size(),
Mathias Agopian1d12d8a2012-09-18 01:38:00 -0700517 mViewport.left, mViewport.top, mViewport.right, mViewport.bottom,
518 mFrame.left, mFrame.top, mFrame.right, mFrame.bottom,
Mathias Agopian766dc492012-10-30 18:08:06 -0700519 mScissor.left, mScissor.top, mScissor.right, mScissor.bottom,
Mathias Agopian1d12d8a2012-09-18 01:38:00 -0700520 tr[0][0], tr[1][0], tr[2][0],
521 tr[0][1], tr[1][1], tr[2][1],
522 tr[0][2], tr[1][2], tr[2][2]);
523
Jesse Hall99c7dbb2013-03-14 14:29:29 -0700524 String8 surfaceDump;
Dan Stozaf10c46e2014-11-11 10:32:31 -0800525 mDisplaySurface->dumpAsString(surfaceDump);
Jesse Hall99c7dbb2013-03-14 14:29:29 -0700526 result.append(surfaceDump);
Mathias Agopian1d12d8a2012-09-18 01:38:00 -0700527}