blob: e3e0b0a768589fefdb614a065fbcff1a0f60e003 [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)
Mathias Agopian92a979a2012-08-02 18:32:23 -070062 : mFlinger(flinger),
Jesse Hallffe1f192013-03-22 15:13:48 -070063 mType(type), mHwcDisplayId(hwcId),
Chih-Wei Huang27e25622013-01-07 17:33:56 +080064 mDisplayToken(displayToken),
Jesse Hall99c7dbb2013-03-14 14:29:29 -070065 mDisplaySurface(displaySurface),
Mathias Agopian92a979a2012-08-02 18:32:23 -070066 mDisplay(EGL_NO_DISPLAY),
67 mSurface(EGL_NO_SURFACE),
Mathias Agopian92a979a2012-08-02 18:32:23 -070068 mDisplayWidth(), mDisplayHeight(), mFormat(),
69 mFlags(),
70 mPageFlipCount(),
Jamie Gennisdd3cb842012-10-19 18:19:11 -070071 mIsSecure(isSecure),
Mathias Agopian92a979a2012-08-02 18:32:23 -070072 mSecureLayerVisible(false),
Jesse Hall01e29052013-02-19 16:13:35 -080073 mLayerStack(NO_LAYER_STACK),
Prashant Malani2c9b11f2014-05-25 01:36:31 -070074 mOrientation(),
Michael Lentine6c9e34a2014-07-14 13:48:55 -070075 mPowerMode(HWC_POWER_MODE_OFF),
76 mActiveConfig(0)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080077{
Jesse Hall83cafff2013-09-16 15:24:53 -070078 mNativeWindow = new Surface(producer, false);
Jesse Hallffe1f192013-03-22 15:13:48 -070079 ANativeWindow* const window = mNativeWindow.get();
80
Jesse Hallffe1f192013-03-22 15:13:48 -070081 /*
82 * Create our display's surface
83 */
84
85 EGLSurface surface;
86 EGLint w, h;
87 EGLDisplay display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
Jesse Hall19e87292013-12-23 21:02:15 -080088 if (config == EGL_NO_CONFIG) {
89 config = RenderEngine::chooseEglConfig(display, format);
90 }
Jesse Hallffe1f192013-03-22 15:13:48 -070091 surface = eglCreateWindowSurface(display, config, window, NULL);
92 eglQuerySurface(display, surface, EGL_WIDTH, &mDisplayWidth);
93 eglQuerySurface(display, surface, EGL_HEIGHT, &mDisplayHeight);
94
John Dong4ee56962014-02-21 12:37:59 -080095 // Make sure that composition can never be stalled by a virtual display
96 // consumer that isn't processing buffers fast enough. We have to do this
97 // in two places:
98 // * Here, in case the display is composed entirely by HWC.
99 // * In makeCurrent(), using eglSwapInterval. Some EGL drivers set the
100 // window's swap interval in eglMakeCurrent, so they'll override the
101 // interval we set here.
102 if (mType >= DisplayDevice::DISPLAY_VIRTUAL)
103 window->setSwapInterval(window, 0);
104
Jesse Hallffe1f192013-03-22 15:13:48 -0700105 mDisplay = display;
106 mSurface = surface;
107 mFormat = format;
108 mPageFlipCount = 0;
109 mViewport.makeInvalid();
110 mFrame.makeInvalid();
111
112 // virtual displays are always considered enabled
Prashant Malani2c9b11f2014-05-25 01:36:31 -0700113 mPowerMode = (mType >= DisplayDevice::DISPLAY_VIRTUAL) ?
114 HWC_POWER_MODE_NORMAL : HWC_POWER_MODE_OFF;
Jesse Hallffe1f192013-03-22 15:13:48 -0700115
116 // Name the display. The name will be replaced shortly if the display
117 // was created with createDisplay().
118 switch (mType) {
119 case DISPLAY_PRIMARY:
120 mDisplayName = "Built-in Screen";
121 break;
122 case DISPLAY_EXTERNAL:
123 mDisplayName = "HDMI Screen";
124 break;
125 default:
126 mDisplayName = "Virtual Screen"; // e.g. Overlay #n
127 break;
128 }
129
130 // initialize the display orientation transform.
131 setProjection(DisplayState::eOrientationDefault, mViewport, mFrame);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800132}
133
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700134DisplayDevice::~DisplayDevice() {
Mathias Agopian92a979a2012-08-02 18:32:23 -0700135 if (mSurface != EGL_NO_SURFACE) {
136 eglDestroySurface(mDisplay, mSurface);
137 mSurface = EGL_NO_SURFACE;
138 }
139}
140
Jesse Hall02d86562013-03-25 14:43:23 -0700141void DisplayDevice::disconnect(HWComposer& hwc) {
142 if (mHwcDisplayId >= 0) {
143 hwc.disconnectDisplay(mHwcDisplayId);
144 if (mHwcDisplayId >= DISPLAY_VIRTUAL)
145 hwc.freeDisplayId(mHwcDisplayId);
146 mHwcDisplayId = -1;
147 }
148}
149
Mathias Agopian92a979a2012-08-02 18:32:23 -0700150bool DisplayDevice::isValid() const {
151 return mFlinger != NULL;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800152}
153
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700154int DisplayDevice::getWidth() const {
Mathias Agopiana4912602012-07-12 14:25:33 -0700155 return mDisplayWidth;
156}
157
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700158int DisplayDevice::getHeight() const {
Mathias Agopiana4912602012-07-12 14:25:33 -0700159 return mDisplayHeight;
160}
161
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700162PixelFormat DisplayDevice::getFormat() const {
Mathias Agopiana4912602012-07-12 14:25:33 -0700163 return mFormat;
164}
165
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700166EGLSurface DisplayDevice::getEGLSurface() const {
Mathias Agopiana4912602012-07-12 14:25:33 -0700167 return mSurface;
168}
169
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700170void DisplayDevice::setDisplayName(const String8& displayName) {
171 if (!displayName.isEmpty()) {
172 // never override the name with an empty name
173 mDisplayName = displayName;
174 }
175}
176
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700177uint32_t DisplayDevice::getPageFlipCount() const {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700178 return mPageFlipCount;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800179}
180
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700181status_t DisplayDevice::compositionComplete() const {
Jesse Hall99c7dbb2013-03-14 14:29:29 -0700182 return mDisplaySurface->compositionComplete();
Mathias Agopian74faca22009-09-17 16:18:16 -0700183}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800184
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700185void DisplayDevice::flip(const Region& dirty) const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800186{
Mathias Agopian875d8e12013-06-07 15:35:48 -0700187 mFlinger->getRenderEngine().checkErrors();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800188
189 EGLDisplay dpy = mDisplay;
190 EGLSurface surface = mSurface;
191
Jesse Hall01e29052013-02-19 16:13:35 -0800192#ifdef EGL_ANDROID_swap_rectangle
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700193 if (mFlags & SWAP_RECTANGLE) {
Mathias Agopianb8a55602009-06-26 19:06:36 -0700194 const Region newDirty(dirty.intersect(bounds()));
195 const Rect b(newDirty.getBounds());
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700196 eglSetSwapRectangleANDROID(dpy, surface,
197 b.left, b.top, b.width(), b.height());
Jesse Hall01e29052013-02-19 16:13:35 -0800198 }
Dan Stozac7014012014-02-14 15:03:43 -0800199#else
200 (void) dirty; // Eliminate unused parameter warning
Mathias Agopian5e78e092009-06-11 17:19:54 -0700201#endif
Mathias Agopiand8707032012-09-18 01:21:55 -0700202
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700203 mPageFlipCount++;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800204}
205
Dan Stoza71433162014-02-04 16:22:36 -0800206status_t DisplayDevice::beginFrame(bool mustRecompose) const {
207 return mDisplaySurface->beginFrame(mustRecompose);
Jesse Hall028dc8f2013-08-20 16:35:32 -0700208}
209
Jesse Hall38efe862013-04-06 23:12:29 -0700210status_t DisplayDevice::prepareFrame(const HWComposer& hwc) const {
211 DisplaySurface::CompositionType compositionType;
212 bool haveGles = hwc.hasGlesComposition(mHwcDisplayId);
213 bool haveHwc = hwc.hasHwcComposition(mHwcDisplayId);
214 if (haveGles && haveHwc) {
215 compositionType = DisplaySurface::COMPOSITION_MIXED;
216 } else if (haveGles) {
217 compositionType = DisplaySurface::COMPOSITION_GLES;
218 } else if (haveHwc) {
219 compositionType = DisplaySurface::COMPOSITION_HWC;
220 } else {
221 // Nothing to do -- when turning the screen off we get a frame like
222 // this. Call it a HWC frame since we won't be doing any GLES work but
223 // will do a prepare/set cycle.
224 compositionType = DisplaySurface::COMPOSITION_HWC;
225 }
226 return mDisplaySurface->prepareFrame(compositionType);
227}
228
Mathias Agopianda27af92012-09-13 18:17:13 -0700229void DisplayDevice::swapBuffers(HWComposer& hwc) const {
Andy McFadden22a99f02013-08-20 16:04:47 -0700230 // We need to call eglSwapBuffers() if:
231 // (1) we don't have a hardware composer, or
232 // (2) we did GLES composition this frame, and either
233 // (a) we have framebuffer target support (not present on legacy
234 // devices, where HWComposer::commit() handles things); or
235 // (b) this is a virtual display
Jesse Hall99c7dbb2013-03-14 14:29:29 -0700236 if (hwc.initCheck() != NO_ERROR ||
237 (hwc.hasGlesComposition(mHwcDisplayId) &&
Andy McFadden22a99f02013-08-20 16:04:47 -0700238 (hwc.supportsFramebufferTarget() || mType >= DISPLAY_VIRTUAL))) {
Jesse Hall99c7dbb2013-03-14 14:29:29 -0700239 EGLBoolean success = eglSwapBuffers(mDisplay, mSurface);
240 if (!success) {
241 EGLint error = eglGetError();
242 if (error == EGL_CONTEXT_LOST ||
243 mType == DisplayDevice::DISPLAY_PRIMARY) {
244 LOG_ALWAYS_FATAL("eglSwapBuffers(%p, %p) failed with 0x%08x",
245 mDisplay, mSurface, error);
246 } else {
247 ALOGE("eglSwapBuffers(%p, %p) failed with 0x%08x",
248 mDisplay, mSurface, error);
Mathias Agopianda27af92012-09-13 18:17:13 -0700249 }
250 }
251 }
Mathias Agopian52e21482012-09-24 18:07:21 -0700252
Jesse Hall99c7dbb2013-03-14 14:29:29 -0700253 status_t result = mDisplaySurface->advanceFrame();
254 if (result != NO_ERROR) {
255 ALOGE("[%s] failed pushing new frame to HWC: %d",
256 mDisplayName.string(), result);
Mathias Agopian32341382012-09-25 19:16:28 -0700257 }
Mathias Agopianda27af92012-09-13 18:17:13 -0700258}
259
260void DisplayDevice::onSwapBuffersCompleted(HWComposer& hwc) const {
261 if (hwc.initCheck() == NO_ERROR) {
Jesse Hall851cfe82013-03-20 13:44:00 -0700262 mDisplaySurface->onFrameCommitted();
Mathias Agopianda27af92012-09-13 18:17:13 -0700263 }
264}
265
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700266uint32_t DisplayDevice::getFlags() const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800267{
268 return mFlags;
269}
270
Mathias Agopian875d8e12013-06-07 15:35:48 -0700271EGLBoolean DisplayDevice::makeCurrent(EGLDisplay dpy, EGLContext ctx) const {
Mathias Agopianda8d0a52012-09-04 15:05:38 -0700272 EGLBoolean result = EGL_TRUE;
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700273 EGLSurface sur = eglGetCurrentSurface(EGL_DRAW);
Mathias Agopian875d8e12013-06-07 15:35:48 -0700274 if (sur != mSurface) {
275 result = eglMakeCurrent(dpy, mSurface, mSurface, ctx);
Mathias Agopianda8d0a52012-09-04 15:05:38 -0700276 if (result == EGL_TRUE) {
Jesse Hallf460f552013-08-06 17:08:53 -0700277 if (mType >= DisplayDevice::DISPLAY_VIRTUAL)
278 eglSwapInterval(dpy, 0);
Mathias Agopianda8d0a52012-09-04 15:05:38 -0700279 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700280 }
Mathias Agopian931bda12013-08-28 18:11:46 -0700281 setViewportAndProjection();
Mathias Agopianda8d0a52012-09-04 15:05:38 -0700282 return result;
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700283}
284
Mathias Agopian875d8e12013-06-07 15:35:48 -0700285void DisplayDevice::setViewportAndProjection() const {
286 size_t w = mDisplayWidth;
287 size_t h = mDisplayHeight;
Dan Stozac1879002014-05-22 15:59:05 -0700288 Rect sourceCrop(0, 0, w, h);
289 mFlinger->getRenderEngine().setViewportAndProjection(w, h, sourceCrop, h, false);
Mathias Agopianbae92d02012-09-28 01:00:47 -0700290}
291
Mathias Agopian1b031492012-06-20 17:51:20 -0700292// ----------------------------------------------------------------------------
293
Mathias Agopian13127d82013-03-05 17:47:11 -0800294void DisplayDevice::setVisibleLayersSortedByZ(const Vector< sp<Layer> >& layers) {
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700295 mVisibleLayersSortedByZ = layers;
Mathias Agopianef7b9c72012-08-10 15:22:19 -0700296 mSecureLayerVisible = false;
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700297 size_t count = layers.size();
298 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -0800299 const sp<Layer>& layer(layers[i]);
Mathias Agopianf5f714a2013-02-26 16:54:05 -0800300 if (layer->isSecure()) {
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700301 mSecureLayerVisible = true;
302 }
303 }
304}
305
Mathias Agopian13127d82013-03-05 17:47:11 -0800306const Vector< sp<Layer> >& DisplayDevice::getVisibleLayersSortedByZ() const {
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700307 return mVisibleLayersSortedByZ;
308}
309
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700310bool DisplayDevice::getSecureLayerVisible() const {
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700311 return mSecureLayerVisible;
312}
313
Mathias Agopiancd60f992012-08-16 16:28:27 -0700314Region DisplayDevice::getDirtyRegion(bool repaintEverything) const {
315 Region dirty;
Mathias Agopiancd60f992012-08-16 16:28:27 -0700316 if (repaintEverything) {
317 dirty.set(getBounds());
318 } else {
Mathias Agopianda8d0a52012-09-04 15:05:38 -0700319 const Transform& planeTransform(mGlobalTransform);
Mathias Agopiancd60f992012-08-16 16:28:27 -0700320 dirty = planeTransform.transform(this->dirtyRegion);
321 dirty.andSelf(getBounds());
322 }
323 return dirty;
324}
325
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700326// ----------------------------------------------------------------------------
Prashant Malani2c9b11f2014-05-25 01:36:31 -0700327void DisplayDevice::setPowerMode(int mode) {
328 mPowerMode = mode;
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700329}
330
Prashant Malani2c9b11f2014-05-25 01:36:31 -0700331int DisplayDevice::getPowerMode() const {
332 return mPowerMode;
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700333}
334
Prashant Malani2c9b11f2014-05-25 01:36:31 -0700335bool DisplayDevice::isDisplayOn() const {
336 return (mPowerMode != HWC_POWER_MODE_OFF);
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700337}
338
339// ----------------------------------------------------------------------------
Michael Lentine6c9e34a2014-07-14 13:48:55 -0700340void DisplayDevice::setActiveConfig(int mode) {
341 mActiveConfig = mode;
342}
343
344int DisplayDevice::getActiveConfig() const {
345 return mActiveConfig;
346}
347
348// ----------------------------------------------------------------------------
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700349
Mathias Agopian28947d72012-08-08 18:51:15 -0700350void DisplayDevice::setLayerStack(uint32_t stack) {
351 mLayerStack = stack;
352 dirtyRegion.set(bounds());
353}
354
355// ----------------------------------------------------------------------------
356
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700357uint32_t DisplayDevice::getOrientationTransform() const {
358 uint32_t transform = 0;
359 switch (mOrientation) {
360 case DisplayState::eOrientationDefault:
361 transform = Transform::ROT_0;
362 break;
363 case DisplayState::eOrientation90:
364 transform = Transform::ROT_90;
365 break;
366 case DisplayState::eOrientation180:
367 transform = Transform::ROT_180;
368 break;
369 case DisplayState::eOrientation270:
370 transform = Transform::ROT_270;
371 break;
372 }
373 return transform;
374}
375
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700376status_t DisplayDevice::orientationToTransfrom(
Mathias Agopian1b031492012-06-20 17:51:20 -0700377 int orientation, int w, int h, Transform* tr)
378{
379 uint32_t flags = 0;
380 switch (orientation) {
Mathias Agopian3165cc22012-08-08 19:42:09 -0700381 case DisplayState::eOrientationDefault:
Mathias Agopian1b031492012-06-20 17:51:20 -0700382 flags = Transform::ROT_0;
383 break;
Mathias Agopian3165cc22012-08-08 19:42:09 -0700384 case DisplayState::eOrientation90:
Mathias Agopian1b031492012-06-20 17:51:20 -0700385 flags = Transform::ROT_90;
386 break;
Mathias Agopian3165cc22012-08-08 19:42:09 -0700387 case DisplayState::eOrientation180:
Mathias Agopian1b031492012-06-20 17:51:20 -0700388 flags = Transform::ROT_180;
389 break;
Mathias Agopian3165cc22012-08-08 19:42:09 -0700390 case DisplayState::eOrientation270:
Mathias Agopian1b031492012-06-20 17:51:20 -0700391 flags = Transform::ROT_270;
392 break;
393 default:
394 return BAD_VALUE;
395 }
396 tr->set(flags, w, h);
397 return NO_ERROR;
398}
399
Mathias Agopian00e8c7a2012-09-04 19:30:46 -0700400void DisplayDevice::setProjection(int orientation,
Mathias Agopianf5f714a2013-02-26 16:54:05 -0800401 const Rect& newViewport, const Rect& newFrame) {
402 Rect viewport(newViewport);
403 Rect frame(newFrame);
404
405 const int w = mDisplayWidth;
406 const int h = mDisplayHeight;
407
408 Transform R;
409 DisplayDevice::orientationToTransfrom(orientation, w, h, &R);
410
411 if (!frame.isValid()) {
412 // the destination frame can be invalid if it has never been set,
413 // in that case we assume the whole display frame.
414 frame = Rect(w, h);
415 }
416
417 if (viewport.isEmpty()) {
418 // viewport can be invalid if it has never been set, in that case
419 // we assume the whole display size.
420 // it's also invalid to have an empty viewport, so we handle that
421 // case in the same way.
422 viewport = Rect(w, h);
423 if (R.getOrientation() & Transform::ROT_90) {
424 // viewport is always specified in the logical orientation
425 // of the display (ie: post-rotation).
426 swap(viewport.right, viewport.bottom);
427 }
428 }
429
430 dirtyRegion.set(getBounds());
431
432 Transform TL, TP, S;
433 float src_width = viewport.width();
434 float src_height = viewport.height();
435 float dst_width = frame.width();
436 float dst_height = frame.height();
437 if (src_width != dst_width || src_height != dst_height) {
438 float sx = dst_width / src_width;
439 float sy = dst_height / src_height;
440 S.set(sx, 0, 0, sy);
441 }
442
443 float src_x = viewport.left;
444 float src_y = viewport.top;
445 float dst_x = frame.left;
446 float dst_y = frame.top;
447 TL.set(-src_x, -src_y);
448 TP.set(dst_x, dst_y);
449
450 // The viewport and frame are both in the logical orientation.
451 // Apply the logical translation, scale to physical size, apply the
452 // physical translation and finally rotate to the physical orientation.
453 mGlobalTransform = R * TP * S * TL;
454
455 const uint8_t type = mGlobalTransform.getType();
456 mNeedsFiltering = (!mGlobalTransform.preserveRects() ||
457 (type >= Transform::SCALE));
458
459 mScissor = mGlobalTransform.transform(viewport);
460 if (mScissor.isEmpty()) {
Mathias Agopian6c7f25a2013-05-09 20:37:10 -0700461 mScissor = getBounds();
Mathias Agopianf5f714a2013-02-26 16:54:05 -0800462 }
463
Mathias Agopianda8d0a52012-09-04 15:05:38 -0700464 mOrientation = orientation;
Mathias Agopian00e8c7a2012-09-04 19:30:46 -0700465 mViewport = viewport;
466 mFrame = frame;
Mathias Agopian1b031492012-06-20 17:51:20 -0700467}
Mathias Agopian1d12d8a2012-09-18 01:38:00 -0700468
Mathias Agopian74d211a2013-04-22 16:55:35 +0200469void DisplayDevice::dump(String8& result) const {
Mathias Agopian1d12d8a2012-09-18 01:38:00 -0700470 const Transform& tr(mGlobalTransform);
Mathias Agopian74d211a2013-04-22 16:55:35 +0200471 result.appendFormat(
Mathias Agopian1d12d8a2012-09-18 01:38:00 -0700472 "+ DisplayDevice: %s\n"
Jesse Hall02d86562013-03-25 14:43:23 -0700473 " type=%x, hwcId=%d, layerStack=%u, (%4dx%4d), ANativeWindow=%p, orient=%2d (type=%08x), "
Michael Lentine6c9e34a2014-07-14 13:48:55 -0700474 "flips=%u, isSecure=%d, secureVis=%d, powerMode=%d, activeConfig=%d, numLayers=%zu\n"
Mathias Agopian766dc492012-10-30 18:08:06 -0700475 " v:[%d,%d,%d,%d], f:[%d,%d,%d,%d], s:[%d,%d,%d,%d],"
Mathias Agopian1d12d8a2012-09-18 01:38:00 -0700476 "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 -0700477 mDisplayName.string(), mType, mHwcDisplayId,
Mathias Agopian1d12d8a2012-09-18 01:38:00 -0700478 mLayerStack, mDisplayWidth, mDisplayHeight, mNativeWindow.get(),
479 mOrientation, tr.getType(), getPageFlipCount(),
Michael Lentine6c9e34a2014-07-14 13:48:55 -0700480 mIsSecure, mSecureLayerVisible, mPowerMode, mActiveConfig,
481 mVisibleLayersSortedByZ.size(),
Mathias Agopian1d12d8a2012-09-18 01:38:00 -0700482 mViewport.left, mViewport.top, mViewport.right, mViewport.bottom,
483 mFrame.left, mFrame.top, mFrame.right, mFrame.bottom,
Mathias Agopian766dc492012-10-30 18:08:06 -0700484 mScissor.left, mScissor.top, mScissor.right, mScissor.bottom,
Mathias Agopian1d12d8a2012-09-18 01:38:00 -0700485 tr[0][0], tr[1][0], tr[2][0],
486 tr[0][1], tr[1][1], tr[2][1],
487 tr[0][2], tr[1][2], tr[2][2]);
488
Jesse Hall99c7dbb2013-03-14 14:29:29 -0700489 String8 surfaceDump;
490 mDisplaySurface->dump(surfaceDump);
491 result.append(surfaceDump);
Mathias Agopian1d12d8a2012-09-18 01:38:00 -0700492}