blob: 9a03e6f29b67adbae01b62f373b9448b944ff074 [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
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080047/*
48 * Initialize the display to the specified values.
49 *
50 */
51
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -070052DisplayDevice::DisplayDevice(
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080053 const sp<SurfaceFlinger>& flinger,
Jamie Gennisdd3cb842012-10-19 18:19:11 -070054 DisplayType type,
Jesse Hallffe1f192013-03-22 15:13:48 -070055 int32_t hwcId,
Jesse Hall19e87292013-12-23 21:02:15 -080056 int format,
Jamie Gennisdd3cb842012-10-19 18:19:11 -070057 bool isSecure,
58 const wp<IBinder>& displayToken,
Jesse Hall99c7dbb2013-03-14 14:29:29 -070059 const sp<DisplaySurface>& displaySurface,
Mathias Agopiandb89edc2013-08-02 01:40:18 -070060 const sp<IGraphicBufferProducer>& producer,
Mathias Agopiana4912602012-07-12 14:25:33 -070061 EGLConfig config)
Jesse Hallb7a05492014-08-14 15:45:06 -070062 : lastCompositionHadVisibleLayers(false),
63 mFlinger(flinger),
Jesse Hallffe1f192013-03-22 15:13:48 -070064 mType(type), mHwcDisplayId(hwcId),
Chih-Wei Huang27e25622013-01-07 17:33:56 +080065 mDisplayToken(displayToken),
Jesse Hall99c7dbb2013-03-14 14:29:29 -070066 mDisplaySurface(displaySurface),
Mathias Agopian92a979a2012-08-02 18:32:23 -070067 mDisplay(EGL_NO_DISPLAY),
68 mSurface(EGL_NO_SURFACE),
Mathias Agopian92a979a2012-08-02 18:32:23 -070069 mDisplayWidth(), mDisplayHeight(), mFormat(),
70 mFlags(),
71 mPageFlipCount(),
Jamie Gennisdd3cb842012-10-19 18:19:11 -070072 mIsSecure(isSecure),
Mathias Agopian92a979a2012-08-02 18:32:23 -070073 mSecureLayerVisible(false),
Jesse Hall01e29052013-02-19 16:13:35 -080074 mLayerStack(NO_LAYER_STACK),
Prashant Malani2c9b11f2014-05-25 01:36:31 -070075 mOrientation(),
Michael Lentine6c9e34a2014-07-14 13:48:55 -070076 mPowerMode(HWC_POWER_MODE_OFF),
77 mActiveConfig(0)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080078{
Jesse Hall83cafff2013-09-16 15:24:53 -070079 mNativeWindow = new Surface(producer, false);
Jesse Hallffe1f192013-03-22 15:13:48 -070080 ANativeWindow* const window = mNativeWindow.get();
81
Jesse Hallffe1f192013-03-22 15:13:48 -070082 /*
83 * Create our display's surface
84 */
85
86 EGLSurface surface;
87 EGLint w, h;
88 EGLDisplay display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
Jesse Hall19e87292013-12-23 21:02:15 -080089 if (config == EGL_NO_CONFIG) {
90 config = RenderEngine::chooseEglConfig(display, format);
91 }
Jesse Hallffe1f192013-03-22 15:13:48 -070092 surface = eglCreateWindowSurface(display, config, window, NULL);
93 eglQuerySurface(display, surface, EGL_WIDTH, &mDisplayWidth);
94 eglQuerySurface(display, surface, EGL_HEIGHT, &mDisplayHeight);
95
John Dong4ee56962014-02-21 12:37:59 -080096 // Make sure that composition can never be stalled by a virtual display
97 // consumer that isn't processing buffers fast enough. We have to do this
98 // in two places:
99 // * Here, in case the display is composed entirely by HWC.
100 // * In makeCurrent(), using eglSwapInterval. Some EGL drivers set the
101 // window's swap interval in eglMakeCurrent, so they'll override the
102 // interval we set here.
103 if (mType >= DisplayDevice::DISPLAY_VIRTUAL)
104 window->setSwapInterval(window, 0);
105
Michael Lentine47e45402014-07-18 15:34:25 -0700106 mConfig = config;
Jesse Hallffe1f192013-03-22 15:13:48 -0700107 mDisplay = display;
108 mSurface = surface;
109 mFormat = format;
110 mPageFlipCount = 0;
111 mViewport.makeInvalid();
112 mFrame.makeInvalid();
113
114 // virtual displays are always considered enabled
Prashant Malani2c9b11f2014-05-25 01:36:31 -0700115 mPowerMode = (mType >= DisplayDevice::DISPLAY_VIRTUAL) ?
116 HWC_POWER_MODE_NORMAL : HWC_POWER_MODE_OFF;
Jesse Hallffe1f192013-03-22 15:13:48 -0700117
118 // Name the display. The name will be replaced shortly if the display
119 // was created with createDisplay().
120 switch (mType) {
121 case DISPLAY_PRIMARY:
122 mDisplayName = "Built-in Screen";
123 break;
124 case DISPLAY_EXTERNAL:
125 mDisplayName = "HDMI Screen";
126 break;
127 default:
128 mDisplayName = "Virtual Screen"; // e.g. Overlay #n
129 break;
130 }
131
132 // initialize the display orientation transform.
133 setProjection(DisplayState::eOrientationDefault, mViewport, mFrame);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800134}
135
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700136DisplayDevice::~DisplayDevice() {
Mathias Agopian92a979a2012-08-02 18:32:23 -0700137 if (mSurface != EGL_NO_SURFACE) {
138 eglDestroySurface(mDisplay, mSurface);
139 mSurface = EGL_NO_SURFACE;
140 }
141}
142
Jesse Hall02d86562013-03-25 14:43:23 -0700143void DisplayDevice::disconnect(HWComposer& hwc) {
144 if (mHwcDisplayId >= 0) {
145 hwc.disconnectDisplay(mHwcDisplayId);
146 if (mHwcDisplayId >= DISPLAY_VIRTUAL)
147 hwc.freeDisplayId(mHwcDisplayId);
148 mHwcDisplayId = -1;
149 }
150}
151
Mathias Agopian92a979a2012-08-02 18:32:23 -0700152bool DisplayDevice::isValid() const {
153 return mFlinger != NULL;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800154}
155
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700156int DisplayDevice::getWidth() const {
Mathias Agopiana4912602012-07-12 14:25:33 -0700157 return mDisplayWidth;
158}
159
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700160int DisplayDevice::getHeight() const {
Mathias Agopiana4912602012-07-12 14:25:33 -0700161 return mDisplayHeight;
162}
163
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700164PixelFormat DisplayDevice::getFormat() const {
Mathias Agopiana4912602012-07-12 14:25:33 -0700165 return mFormat;
166}
167
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700168EGLSurface DisplayDevice::getEGLSurface() const {
Mathias Agopiana4912602012-07-12 14:25:33 -0700169 return mSurface;
170}
171
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700172void DisplayDevice::setDisplayName(const String8& displayName) {
173 if (!displayName.isEmpty()) {
174 // never override the name with an empty name
175 mDisplayName = displayName;
176 }
177}
178
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700179uint32_t DisplayDevice::getPageFlipCount() const {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700180 return mPageFlipCount;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800181}
182
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700183status_t DisplayDevice::compositionComplete() const {
Jesse Hall99c7dbb2013-03-14 14:29:29 -0700184 return mDisplaySurface->compositionComplete();
Mathias Agopian74faca22009-09-17 16:18:16 -0700185}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800186
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700187void DisplayDevice::flip(const Region& dirty) const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800188{
Mathias Agopian875d8e12013-06-07 15:35:48 -0700189 mFlinger->getRenderEngine().checkErrors();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800190
191 EGLDisplay dpy = mDisplay;
192 EGLSurface surface = mSurface;
193
Jesse Hall01e29052013-02-19 16:13:35 -0800194#ifdef EGL_ANDROID_swap_rectangle
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700195 if (mFlags & SWAP_RECTANGLE) {
Mathias Agopianb8a55602009-06-26 19:06:36 -0700196 const Region newDirty(dirty.intersect(bounds()));
197 const Rect b(newDirty.getBounds());
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700198 eglSetSwapRectangleANDROID(dpy, surface,
199 b.left, b.top, b.width(), b.height());
Jesse Hall01e29052013-02-19 16:13:35 -0800200 }
Dan Stozac7014012014-02-14 15:03:43 -0800201#else
202 (void) dirty; // Eliminate unused parameter warning
Mathias Agopian5e78e092009-06-11 17:19:54 -0700203#endif
Mathias Agopiand8707032012-09-18 01:21:55 -0700204
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700205 mPageFlipCount++;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800206}
207
Dan Stoza71433162014-02-04 16:22:36 -0800208status_t DisplayDevice::beginFrame(bool mustRecompose) const {
209 return mDisplaySurface->beginFrame(mustRecompose);
Jesse Hall028dc8f2013-08-20 16:35:32 -0700210}
211
Jesse Hall38efe862013-04-06 23:12:29 -0700212status_t DisplayDevice::prepareFrame(const HWComposer& hwc) const {
213 DisplaySurface::CompositionType compositionType;
214 bool haveGles = hwc.hasGlesComposition(mHwcDisplayId);
215 bool haveHwc = hwc.hasHwcComposition(mHwcDisplayId);
216 if (haveGles && haveHwc) {
217 compositionType = DisplaySurface::COMPOSITION_MIXED;
218 } else if (haveGles) {
219 compositionType = DisplaySurface::COMPOSITION_GLES;
220 } else if (haveHwc) {
221 compositionType = DisplaySurface::COMPOSITION_HWC;
222 } else {
223 // Nothing to do -- when turning the screen off we get a frame like
224 // this. Call it a HWC frame since we won't be doing any GLES work but
225 // will do a prepare/set cycle.
226 compositionType = DisplaySurface::COMPOSITION_HWC;
227 }
228 return mDisplaySurface->prepareFrame(compositionType);
229}
230
Mathias Agopianda27af92012-09-13 18:17:13 -0700231void DisplayDevice::swapBuffers(HWComposer& hwc) const {
Andy McFadden22a99f02013-08-20 16:04:47 -0700232 // We need to call eglSwapBuffers() if:
233 // (1) we don't have a hardware composer, or
234 // (2) we did GLES composition this frame, and either
235 // (a) we have framebuffer target support (not present on legacy
236 // devices, where HWComposer::commit() handles things); or
237 // (b) this is a virtual display
Jesse Hall99c7dbb2013-03-14 14:29:29 -0700238 if (hwc.initCheck() != NO_ERROR ||
239 (hwc.hasGlesComposition(mHwcDisplayId) &&
Andy McFadden22a99f02013-08-20 16:04:47 -0700240 (hwc.supportsFramebufferTarget() || mType >= DISPLAY_VIRTUAL))) {
Jesse Hall99c7dbb2013-03-14 14:29:29 -0700241 EGLBoolean success = eglSwapBuffers(mDisplay, mSurface);
242 if (!success) {
243 EGLint error = eglGetError();
244 if (error == EGL_CONTEXT_LOST ||
245 mType == DisplayDevice::DISPLAY_PRIMARY) {
246 LOG_ALWAYS_FATAL("eglSwapBuffers(%p, %p) failed with 0x%08x",
247 mDisplay, mSurface, error);
248 } else {
249 ALOGE("eglSwapBuffers(%p, %p) failed with 0x%08x",
250 mDisplay, mSurface, error);
Mathias Agopianda27af92012-09-13 18:17:13 -0700251 }
252 }
253 }
Mathias Agopian52e21482012-09-24 18:07:21 -0700254
Jesse Hall99c7dbb2013-03-14 14:29:29 -0700255 status_t result = mDisplaySurface->advanceFrame();
256 if (result != NO_ERROR) {
257 ALOGE("[%s] failed pushing new frame to HWC: %d",
258 mDisplayName.string(), result);
Mathias Agopian32341382012-09-25 19:16:28 -0700259 }
Mathias Agopianda27af92012-09-13 18:17:13 -0700260}
261
262void DisplayDevice::onSwapBuffersCompleted(HWComposer& hwc) const {
263 if (hwc.initCheck() == NO_ERROR) {
Jesse Hall851cfe82013-03-20 13:44:00 -0700264 mDisplaySurface->onFrameCommitted();
Mathias Agopianda27af92012-09-13 18:17:13 -0700265 }
266}
267
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700268uint32_t DisplayDevice::getFlags() const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800269{
270 return mFlags;
271}
272
Mathias Agopian875d8e12013-06-07 15:35:48 -0700273EGLBoolean DisplayDevice::makeCurrent(EGLDisplay dpy, EGLContext ctx) const {
Mathias Agopianda8d0a52012-09-04 15:05:38 -0700274 EGLBoolean result = EGL_TRUE;
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700275 EGLSurface sur = eglGetCurrentSurface(EGL_DRAW);
Mathias Agopian875d8e12013-06-07 15:35:48 -0700276 if (sur != mSurface) {
277 result = eglMakeCurrent(dpy, mSurface, mSurface, ctx);
Mathias Agopianda8d0a52012-09-04 15:05:38 -0700278 if (result == EGL_TRUE) {
Jesse Hallf460f552013-08-06 17:08:53 -0700279 if (mType >= DisplayDevice::DISPLAY_VIRTUAL)
280 eglSwapInterval(dpy, 0);
Mathias Agopianda8d0a52012-09-04 15:05:38 -0700281 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700282 }
Mathias Agopian931bda12013-08-28 18:11:46 -0700283 setViewportAndProjection();
Mathias Agopianda8d0a52012-09-04 15:05:38 -0700284 return result;
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700285}
286
Mathias Agopian875d8e12013-06-07 15:35:48 -0700287void DisplayDevice::setViewportAndProjection() const {
288 size_t w = mDisplayWidth;
289 size_t h = mDisplayHeight;
Dan Stozac1879002014-05-22 15:59:05 -0700290 Rect sourceCrop(0, 0, w, h);
Riley Andrewsc3ebe662014-09-04 16:20:31 -0700291 mFlinger->getRenderEngine().setViewportAndProjection(w, h, sourceCrop, h,
292 false, Transform::ROT_0);
Mathias Agopianbae92d02012-09-28 01:00:47 -0700293}
294
Mathias Agopian1b031492012-06-20 17:51:20 -0700295// ----------------------------------------------------------------------------
296
Mathias Agopian13127d82013-03-05 17:47:11 -0800297void DisplayDevice::setVisibleLayersSortedByZ(const Vector< sp<Layer> >& layers) {
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700298 mVisibleLayersSortedByZ = layers;
Mathias Agopianef7b9c72012-08-10 15:22:19 -0700299 mSecureLayerVisible = false;
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700300 size_t count = layers.size();
301 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -0800302 const sp<Layer>& layer(layers[i]);
Mathias Agopianf5f714a2013-02-26 16:54:05 -0800303 if (layer->isSecure()) {
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700304 mSecureLayerVisible = true;
305 }
306 }
307}
308
Mathias Agopian13127d82013-03-05 17:47:11 -0800309const Vector< sp<Layer> >& DisplayDevice::getVisibleLayersSortedByZ() const {
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700310 return mVisibleLayersSortedByZ;
311}
312
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700313bool DisplayDevice::getSecureLayerVisible() const {
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700314 return mSecureLayerVisible;
315}
316
Mathias Agopiancd60f992012-08-16 16:28:27 -0700317Region DisplayDevice::getDirtyRegion(bool repaintEverything) const {
318 Region dirty;
Mathias Agopiancd60f992012-08-16 16:28:27 -0700319 if (repaintEverything) {
320 dirty.set(getBounds());
321 } else {
Mathias Agopianda8d0a52012-09-04 15:05:38 -0700322 const Transform& planeTransform(mGlobalTransform);
Mathias Agopiancd60f992012-08-16 16:28:27 -0700323 dirty = planeTransform.transform(this->dirtyRegion);
324 dirty.andSelf(getBounds());
325 }
326 return dirty;
327}
328
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700329// ----------------------------------------------------------------------------
Prashant Malani2c9b11f2014-05-25 01:36:31 -0700330void DisplayDevice::setPowerMode(int mode) {
331 mPowerMode = mode;
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700332}
333
Prashant Malani2c9b11f2014-05-25 01:36:31 -0700334int DisplayDevice::getPowerMode() const {
335 return mPowerMode;
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700336}
337
Prashant Malani2c9b11f2014-05-25 01:36:31 -0700338bool DisplayDevice::isDisplayOn() const {
339 return (mPowerMode != HWC_POWER_MODE_OFF);
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700340}
341
342// ----------------------------------------------------------------------------
Michael Lentine6c9e34a2014-07-14 13:48:55 -0700343void DisplayDevice::setActiveConfig(int mode) {
344 mActiveConfig = mode;
345}
346
347int DisplayDevice::getActiveConfig() const {
348 return mActiveConfig;
349}
350
351// ----------------------------------------------------------------------------
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700352
Mathias Agopian28947d72012-08-08 18:51:15 -0700353void DisplayDevice::setLayerStack(uint32_t stack) {
354 mLayerStack = stack;
355 dirtyRegion.set(bounds());
356}
357
358// ----------------------------------------------------------------------------
359
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700360uint32_t DisplayDevice::getOrientationTransform() const {
361 uint32_t transform = 0;
362 switch (mOrientation) {
363 case DisplayState::eOrientationDefault:
364 transform = Transform::ROT_0;
365 break;
366 case DisplayState::eOrientation90:
367 transform = Transform::ROT_90;
368 break;
369 case DisplayState::eOrientation180:
370 transform = Transform::ROT_180;
371 break;
372 case DisplayState::eOrientation270:
373 transform = Transform::ROT_270;
374 break;
375 }
376 return transform;
377}
378
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700379status_t DisplayDevice::orientationToTransfrom(
Mathias Agopian1b031492012-06-20 17:51:20 -0700380 int orientation, int w, int h, Transform* tr)
381{
382 uint32_t flags = 0;
383 switch (orientation) {
Mathias Agopian3165cc22012-08-08 19:42:09 -0700384 case DisplayState::eOrientationDefault:
Mathias Agopian1b031492012-06-20 17:51:20 -0700385 flags = Transform::ROT_0;
386 break;
Mathias Agopian3165cc22012-08-08 19:42:09 -0700387 case DisplayState::eOrientation90:
Mathias Agopian1b031492012-06-20 17:51:20 -0700388 flags = Transform::ROT_90;
389 break;
Mathias Agopian3165cc22012-08-08 19:42:09 -0700390 case DisplayState::eOrientation180:
Mathias Agopian1b031492012-06-20 17:51:20 -0700391 flags = Transform::ROT_180;
392 break;
Mathias Agopian3165cc22012-08-08 19:42:09 -0700393 case DisplayState::eOrientation270:
Mathias Agopian1b031492012-06-20 17:51:20 -0700394 flags = Transform::ROT_270;
395 break;
396 default:
397 return BAD_VALUE;
398 }
399 tr->set(flags, w, h);
400 return NO_ERROR;
401}
402
Michael Lentine47e45402014-07-18 15:34:25 -0700403void DisplayDevice::setDisplaySize(const int newWidth, const int newHeight) {
404 dirtyRegion.set(getBounds());
405
Michael Lentinef2568de2014-08-20 10:51:23 -0700406 if (mSurface != EGL_NO_SURFACE) {
407 eglDestroySurface(mDisplay, mSurface);
408 mSurface = EGL_NO_SURFACE;
409 }
410
Michael Lentine47e45402014-07-18 15:34:25 -0700411 mDisplaySurface->resizeBuffers(newWidth, newHeight);
412
413 ANativeWindow* const window = mNativeWindow.get();
414 mSurface = eglCreateWindowSurface(mDisplay, mConfig, window, NULL);
415 eglQuerySurface(mDisplay, mSurface, EGL_WIDTH, &mDisplayWidth);
416 eglQuerySurface(mDisplay, mSurface, EGL_HEIGHT, &mDisplayHeight);
417
418 LOG_FATAL_IF(mDisplayWidth != newWidth,
419 "Unable to set new width to %d", newWidth);
420 LOG_FATAL_IF(mDisplayHeight != newHeight,
421 "Unable to set new height to %d", newHeight);
422}
423
Mathias Agopian00e8c7a2012-09-04 19:30:46 -0700424void DisplayDevice::setProjection(int orientation,
Mathias Agopianf5f714a2013-02-26 16:54:05 -0800425 const Rect& newViewport, const Rect& newFrame) {
426 Rect viewport(newViewport);
427 Rect frame(newFrame);
428
429 const int w = mDisplayWidth;
430 const int h = mDisplayHeight;
431
432 Transform R;
433 DisplayDevice::orientationToTransfrom(orientation, w, h, &R);
434
435 if (!frame.isValid()) {
436 // the destination frame can be invalid if it has never been set,
437 // in that case we assume the whole display frame.
438 frame = Rect(w, h);
439 }
440
441 if (viewport.isEmpty()) {
442 // viewport can be invalid if it has never been set, in that case
443 // we assume the whole display size.
444 // it's also invalid to have an empty viewport, so we handle that
445 // case in the same way.
446 viewport = Rect(w, h);
447 if (R.getOrientation() & Transform::ROT_90) {
448 // viewport is always specified in the logical orientation
449 // of the display (ie: post-rotation).
450 swap(viewport.right, viewport.bottom);
451 }
452 }
453
454 dirtyRegion.set(getBounds());
455
456 Transform TL, TP, S;
457 float src_width = viewport.width();
458 float src_height = viewport.height();
459 float dst_width = frame.width();
460 float dst_height = frame.height();
461 if (src_width != dst_width || src_height != dst_height) {
462 float sx = dst_width / src_width;
463 float sy = dst_height / src_height;
464 S.set(sx, 0, 0, sy);
465 }
466
467 float src_x = viewport.left;
468 float src_y = viewport.top;
469 float dst_x = frame.left;
470 float dst_y = frame.top;
471 TL.set(-src_x, -src_y);
472 TP.set(dst_x, dst_y);
473
474 // The viewport and frame are both in the logical orientation.
475 // Apply the logical translation, scale to physical size, apply the
476 // physical translation and finally rotate to the physical orientation.
477 mGlobalTransform = R * TP * S * TL;
478
479 const uint8_t type = mGlobalTransform.getType();
480 mNeedsFiltering = (!mGlobalTransform.preserveRects() ||
481 (type >= Transform::SCALE));
482
483 mScissor = mGlobalTransform.transform(viewport);
484 if (mScissor.isEmpty()) {
Mathias Agopian6c7f25a2013-05-09 20:37:10 -0700485 mScissor = getBounds();
Mathias Agopianf5f714a2013-02-26 16:54:05 -0800486 }
487
Mathias Agopianda8d0a52012-09-04 15:05:38 -0700488 mOrientation = orientation;
Mathias Agopian00e8c7a2012-09-04 19:30:46 -0700489 mViewport = viewport;
490 mFrame = frame;
Mathias Agopian1b031492012-06-20 17:51:20 -0700491}
Mathias Agopian1d12d8a2012-09-18 01:38:00 -0700492
Mathias Agopian74d211a2013-04-22 16:55:35 +0200493void DisplayDevice::dump(String8& result) const {
Mathias Agopian1d12d8a2012-09-18 01:38:00 -0700494 const Transform& tr(mGlobalTransform);
Mathias Agopian74d211a2013-04-22 16:55:35 +0200495 result.appendFormat(
Mathias Agopian1d12d8a2012-09-18 01:38:00 -0700496 "+ DisplayDevice: %s\n"
Jesse Hall02d86562013-03-25 14:43:23 -0700497 " type=%x, hwcId=%d, layerStack=%u, (%4dx%4d), ANativeWindow=%p, orient=%2d (type=%08x), "
Michael Lentine6c9e34a2014-07-14 13:48:55 -0700498 "flips=%u, isSecure=%d, secureVis=%d, powerMode=%d, activeConfig=%d, numLayers=%zu\n"
Mathias Agopian766dc492012-10-30 18:08:06 -0700499 " v:[%d,%d,%d,%d], f:[%d,%d,%d,%d], s:[%d,%d,%d,%d],"
Mathias Agopian1d12d8a2012-09-18 01:38:00 -0700500 "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 -0700501 mDisplayName.string(), mType, mHwcDisplayId,
Mathias Agopian1d12d8a2012-09-18 01:38:00 -0700502 mLayerStack, mDisplayWidth, mDisplayHeight, mNativeWindow.get(),
503 mOrientation, tr.getType(), getPageFlipCount(),
Michael Lentine6c9e34a2014-07-14 13:48:55 -0700504 mIsSecure, mSecureLayerVisible, mPowerMode, mActiveConfig,
505 mVisibleLayersSortedByZ.size(),
Mathias Agopian1d12d8a2012-09-18 01:38:00 -0700506 mViewport.left, mViewport.top, mViewport.right, mViewport.bottom,
507 mFrame.left, mFrame.top, mFrame.right, mFrame.bottom,
Mathias Agopian766dc492012-10-30 18:08:06 -0700508 mScissor.left, mScissor.top, mScissor.right, mScissor.bottom,
Mathias Agopian1d12d8a2012-09-18 01:38:00 -0700509 tr[0][0], tr[1][0], tr[2][0],
510 tr[0][1], tr[1][1], tr[2][1],
511 tr[0][2], tr[1][2], tr[2][2]);
512
Jesse Hall99c7dbb2013-03-14 14:29:29 -0700513 String8 surfaceDump;
Dan Stozaf10c46e2014-11-11 10:32:31 -0800514 mDisplaySurface->dumpAsString(surfaceDump);
Jesse Hall99c7dbb2013-03-14 14:29:29 -0700515 result.append(surfaceDump);
Mathias Agopian1d12d8a2012-09-18 01:38:00 -0700516}