The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* |
| 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 Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 17 | #include <stdlib.h> |
| 18 | #include <stdio.h> |
| 19 | #include <string.h> |
| 20 | #include <math.h> |
| 21 | |
| 22 | #include <cutils/properties.h> |
| 23 | |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 24 | #include <utils/RefBase.h> |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 25 | #include <utils/Log.h> |
| 26 | |
Mathias Agopian | c666cae | 2012-07-25 18:56:13 -0700 | [diff] [blame] | 27 | #include <ui/DisplayInfo.h> |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 28 | #include <ui/PixelFormat.h> |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 29 | |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 30 | #include <gui/Surface.h> |
Jamie Gennis | 1a4d883 | 2012-08-02 20:11:05 -0700 | [diff] [blame] | 31 | |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 32 | #include <hardware/gralloc.h> |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 33 | |
Jesse Hall | 99c7dbb | 2013-03-14 14:29:29 -0700 | [diff] [blame] | 34 | #include "DisplayHardware/DisplaySurface.h" |
Mathias Agopian | 1b03149 | 2012-06-20 17:51:20 -0700 | [diff] [blame] | 35 | #include "DisplayHardware/HWComposer.h" |
Mathias Agopian | 875d8e1 | 2013-06-07 15:35:48 -0700 | [diff] [blame] | 36 | #include "RenderEngine/RenderEngine.h" |
Mathias Agopian | 1b03149 | 2012-06-20 17:51:20 -0700 | [diff] [blame] | 37 | |
Mathias Agopian | da8d0a5 | 2012-09-04 15:05:38 -0700 | [diff] [blame] | 38 | #include "clz.h" |
Mathias Agopian | 0f2f5ff | 2012-07-31 23:09:07 -0700 | [diff] [blame] | 39 | #include "DisplayDevice.h" |
Mathias Agopian | c7d14e2 | 2011-08-01 16:32:21 -0700 | [diff] [blame] | 40 | #include "SurfaceFlinger.h" |
Mathias Agopian | 13127d8 | 2013-03-05 17:47:11 -0800 | [diff] [blame] | 41 | #include "Layer.h" |
Mathias Agopian | 1f7bec6 | 2010-06-25 18:02:21 -0700 | [diff] [blame] | 42 | |
Mathias Agopian | a491260 | 2012-07-12 14:25:33 -0700 | [diff] [blame] | 43 | // ---------------------------------------------------------------------------- |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 44 | using namespace android; |
Mathias Agopian | a491260 | 2012-07-12 14:25:33 -0700 | [diff] [blame] | 45 | // ---------------------------------------------------------------------------- |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 46 | |
Andreas Gampe | 89fd4f7 | 2014-11-13 14:18:56 -0800 | [diff] [blame] | 47 | #ifdef EGL_ANDROID_swap_rectangle |
| 48 | static constexpr bool kEGLAndroidSwapRectangle = true; |
| 49 | #else |
| 50 | static 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. |
| 55 | inline void eglSetSwapRectangleANDROID (EGLDisplay, EGLSurface, EGLint, EGLint, EGLint, EGLint) { |
| 56 | } |
| 57 | #endif |
| 58 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 59 | /* |
| 60 | * Initialize the display to the specified values. |
| 61 | * |
| 62 | */ |
| 63 | |
Mathias Agopian | 0f2f5ff | 2012-07-31 23:09:07 -0700 | [diff] [blame] | 64 | DisplayDevice::DisplayDevice( |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 65 | const sp<SurfaceFlinger>& flinger, |
Jamie Gennis | dd3cb84 | 2012-10-19 18:19:11 -0700 | [diff] [blame] | 66 | DisplayType type, |
Jesse Hall | ffe1f19 | 2013-03-22 15:13:48 -0700 | [diff] [blame] | 67 | int32_t hwcId, |
Jesse Hall | 19e8729 | 2013-12-23 21:02:15 -0800 | [diff] [blame] | 68 | int format, |
Jamie Gennis | dd3cb84 | 2012-10-19 18:19:11 -0700 | [diff] [blame] | 69 | bool isSecure, |
| 70 | const wp<IBinder>& displayToken, |
Jesse Hall | 99c7dbb | 2013-03-14 14:29:29 -0700 | [diff] [blame] | 71 | const sp<DisplaySurface>& displaySurface, |
Mathias Agopian | db89edc | 2013-08-02 01:40:18 -0700 | [diff] [blame] | 72 | const sp<IGraphicBufferProducer>& producer, |
Mathias Agopian | a491260 | 2012-07-12 14:25:33 -0700 | [diff] [blame] | 73 | EGLConfig config) |
Jesse Hall | b7a0549 | 2014-08-14 15:45:06 -0700 | [diff] [blame] | 74 | : lastCompositionHadVisibleLayers(false), |
| 75 | mFlinger(flinger), |
Jesse Hall | ffe1f19 | 2013-03-22 15:13:48 -0700 | [diff] [blame] | 76 | mType(type), mHwcDisplayId(hwcId), |
Chih-Wei Huang | 27e2562 | 2013-01-07 17:33:56 +0800 | [diff] [blame] | 77 | mDisplayToken(displayToken), |
Jesse Hall | 99c7dbb | 2013-03-14 14:29:29 -0700 | [diff] [blame] | 78 | mDisplaySurface(displaySurface), |
Mathias Agopian | 92a979a | 2012-08-02 18:32:23 -0700 | [diff] [blame] | 79 | mDisplay(EGL_NO_DISPLAY), |
| 80 | mSurface(EGL_NO_SURFACE), |
Mathias Agopian | 92a979a | 2012-08-02 18:32:23 -0700 | [diff] [blame] | 81 | mDisplayWidth(), mDisplayHeight(), mFormat(), |
| 82 | mFlags(), |
| 83 | mPageFlipCount(), |
Jamie Gennis | dd3cb84 | 2012-10-19 18:19:11 -0700 | [diff] [blame] | 84 | mIsSecure(isSecure), |
Mathias Agopian | 92a979a | 2012-08-02 18:32:23 -0700 | [diff] [blame] | 85 | mSecureLayerVisible(false), |
Jesse Hall | 01e2905 | 2013-02-19 16:13:35 -0800 | [diff] [blame] | 86 | mLayerStack(NO_LAYER_STACK), |
Prashant Malani | 2c9b11f | 2014-05-25 01:36:31 -0700 | [diff] [blame] | 87 | mOrientation(), |
Michael Lentine | 6c9e34a | 2014-07-14 13:48:55 -0700 | [diff] [blame] | 88 | mPowerMode(HWC_POWER_MODE_OFF), |
| 89 | mActiveConfig(0) |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 90 | { |
james.zhang | 5e8eb5e | 2015-12-01 17:55:11 +0800 | [diff] [blame] | 91 | Surface* surface; |
| 92 | mNativeWindow = surface = new Surface(producer, false); |
Jesse Hall | ffe1f19 | 2013-03-22 15:13:48 -0700 | [diff] [blame] | 93 | ANativeWindow* const window = mNativeWindow.get(); |
| 94 | |
Jesse Hall | ffe1f19 | 2013-03-22 15:13:48 -0700 | [diff] [blame] | 95 | /* |
| 96 | * Create our display's surface |
| 97 | */ |
| 98 | |
james.zhang | 5e8eb5e | 2015-12-01 17:55:11 +0800 | [diff] [blame] | 99 | EGLSurface eglSurface; |
Jesse Hall | ffe1f19 | 2013-03-22 15:13:48 -0700 | [diff] [blame] | 100 | EGLDisplay display = eglGetDisplay(EGL_DEFAULT_DISPLAY); |
Jesse Hall | 19e8729 | 2013-12-23 21:02:15 -0800 | [diff] [blame] | 101 | if (config == EGL_NO_CONFIG) { |
| 102 | config = RenderEngine::chooseEglConfig(display, format); |
| 103 | } |
james.zhang | 5e8eb5e | 2015-12-01 17:55:11 +0800 | [diff] [blame] | 104 | eglSurface = eglCreateWindowSurface(display, config, window, NULL); |
| 105 | eglQuerySurface(display, eglSurface, EGL_WIDTH, &mDisplayWidth); |
| 106 | eglQuerySurface(display, eglSurface, EGL_HEIGHT, &mDisplayHeight); |
Jesse Hall | ffe1f19 | 2013-03-22 15:13:48 -0700 | [diff] [blame] | 107 | |
John Dong | 4ee5696 | 2014-02-21 12:37:59 -0800 | [diff] [blame] | 108 | // 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 Lentine | 47e4540 | 2014-07-18 15:34:25 -0700 | [diff] [blame] | 118 | mConfig = config; |
Jesse Hall | ffe1f19 | 2013-03-22 15:13:48 -0700 | [diff] [blame] | 119 | mDisplay = display; |
james.zhang | 5e8eb5e | 2015-12-01 17:55:11 +0800 | [diff] [blame] | 120 | mSurface = eglSurface; |
Jesse Hall | ffe1f19 | 2013-03-22 15:13:48 -0700 | [diff] [blame] | 121 | mFormat = format; |
| 122 | mPageFlipCount = 0; |
| 123 | mViewport.makeInvalid(); |
| 124 | mFrame.makeInvalid(); |
| 125 | |
| 126 | // virtual displays are always considered enabled |
Prashant Malani | 2c9b11f | 2014-05-25 01:36:31 -0700 | [diff] [blame] | 127 | mPowerMode = (mType >= DisplayDevice::DISPLAY_VIRTUAL) ? |
| 128 | HWC_POWER_MODE_NORMAL : HWC_POWER_MODE_OFF; |
Jesse Hall | ffe1f19 | 2013-03-22 15:13:48 -0700 | [diff] [blame] | 129 | |
| 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.zhang | 5e8eb5e | 2015-12-01 17:55:11 +0800 | [diff] [blame] | 146 | |
| 147 | #ifdef NUM_FRAMEBUFFER_SURFACE_BUFFERS |
| 148 | surface->allocateBuffers(); |
| 149 | #endif |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 150 | } |
| 151 | |
Mathias Agopian | 0f2f5ff | 2012-07-31 23:09:07 -0700 | [diff] [blame] | 152 | DisplayDevice::~DisplayDevice() { |
Mathias Agopian | 92a979a | 2012-08-02 18:32:23 -0700 | [diff] [blame] | 153 | if (mSurface != EGL_NO_SURFACE) { |
| 154 | eglDestroySurface(mDisplay, mSurface); |
| 155 | mSurface = EGL_NO_SURFACE; |
| 156 | } |
| 157 | } |
| 158 | |
Jesse Hall | 02d8656 | 2013-03-25 14:43:23 -0700 | [diff] [blame] | 159 | void 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 Agopian | 92a979a | 2012-08-02 18:32:23 -0700 | [diff] [blame] | 168 | bool DisplayDevice::isValid() const { |
| 169 | return mFlinger != NULL; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 170 | } |
| 171 | |
Mathias Agopian | 0f2f5ff | 2012-07-31 23:09:07 -0700 | [diff] [blame] | 172 | int DisplayDevice::getWidth() const { |
Mathias Agopian | a491260 | 2012-07-12 14:25:33 -0700 | [diff] [blame] | 173 | return mDisplayWidth; |
| 174 | } |
| 175 | |
Mathias Agopian | 0f2f5ff | 2012-07-31 23:09:07 -0700 | [diff] [blame] | 176 | int DisplayDevice::getHeight() const { |
Mathias Agopian | a491260 | 2012-07-12 14:25:33 -0700 | [diff] [blame] | 177 | return mDisplayHeight; |
| 178 | } |
| 179 | |
Mathias Agopian | 0f2f5ff | 2012-07-31 23:09:07 -0700 | [diff] [blame] | 180 | PixelFormat DisplayDevice::getFormat() const { |
Mathias Agopian | a491260 | 2012-07-12 14:25:33 -0700 | [diff] [blame] | 181 | return mFormat; |
| 182 | } |
| 183 | |
Mathias Agopian | 0f2f5ff | 2012-07-31 23:09:07 -0700 | [diff] [blame] | 184 | EGLSurface DisplayDevice::getEGLSurface() const { |
Mathias Agopian | a491260 | 2012-07-12 14:25:33 -0700 | [diff] [blame] | 185 | return mSurface; |
| 186 | } |
| 187 | |
Mathias Agopian | 9e2463e | 2012-09-21 18:26:16 -0700 | [diff] [blame] | 188 | void 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 Agopian | 0f2f5ff | 2012-07-31 23:09:07 -0700 | [diff] [blame] | 195 | uint32_t DisplayDevice::getPageFlipCount() const { |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 196 | return mPageFlipCount; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 197 | } |
| 198 | |
Mathias Agopian | 0f2f5ff | 2012-07-31 23:09:07 -0700 | [diff] [blame] | 199 | status_t DisplayDevice::compositionComplete() const { |
Jesse Hall | 99c7dbb | 2013-03-14 14:29:29 -0700 | [diff] [blame] | 200 | return mDisplaySurface->compositionComplete(); |
Mathias Agopian | 74faca2 | 2009-09-17 16:18:16 -0700 | [diff] [blame] | 201 | } |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 202 | |
Mathias Agopian | 0f2f5ff | 2012-07-31 23:09:07 -0700 | [diff] [blame] | 203 | void DisplayDevice::flip(const Region& dirty) const |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 204 | { |
Mathias Agopian | 875d8e1 | 2013-06-07 15:35:48 -0700 | [diff] [blame] | 205 | mFlinger->getRenderEngine().checkErrors(); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 206 | |
Andreas Gampe | 89fd4f7 | 2014-11-13 14:18:56 -0800 | [diff] [blame] | 207 | 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 Hall | 01e2905 | 2013-02-19 16:13:35 -0800 | [diff] [blame] | 214 | } |
Mathias Agopian | d870703 | 2012-09-18 01:21:55 -0700 | [diff] [blame] | 215 | |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 216 | mPageFlipCount++; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 217 | } |
| 218 | |
Dan Stoza | 7143316 | 2014-02-04 16:22:36 -0800 | [diff] [blame] | 219 | status_t DisplayDevice::beginFrame(bool mustRecompose) const { |
| 220 | return mDisplaySurface->beginFrame(mustRecompose); |
Jesse Hall | 028dc8f | 2013-08-20 16:35:32 -0700 | [diff] [blame] | 221 | } |
| 222 | |
Jesse Hall | 38efe86 | 2013-04-06 23:12:29 -0700 | [diff] [blame] | 223 | status_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 Agopian | da27af9 | 2012-09-13 18:17:13 -0700 | [diff] [blame] | 242 | void DisplayDevice::swapBuffers(HWComposer& hwc) const { |
Andy McFadden | 22a99f0 | 2013-08-20 16:04:47 -0700 | [diff] [blame] | 243 | // 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 Hall | 99c7dbb | 2013-03-14 14:29:29 -0700 | [diff] [blame] | 249 | if (hwc.initCheck() != NO_ERROR || |
| 250 | (hwc.hasGlesComposition(mHwcDisplayId) && |
Andy McFadden | 22a99f0 | 2013-08-20 16:04:47 -0700 | [diff] [blame] | 251 | (hwc.supportsFramebufferTarget() || mType >= DISPLAY_VIRTUAL))) { |
Jesse Hall | 99c7dbb | 2013-03-14 14:29:29 -0700 | [diff] [blame] | 252 | 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 Agopian | da27af9 | 2012-09-13 18:17:13 -0700 | [diff] [blame] | 262 | } |
| 263 | } |
| 264 | } |
Mathias Agopian | 52e2148 | 2012-09-24 18:07:21 -0700 | [diff] [blame] | 265 | |
Jesse Hall | 99c7dbb | 2013-03-14 14:29:29 -0700 | [diff] [blame] | 266 | 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 Agopian | 3234138 | 2012-09-25 19:16:28 -0700 | [diff] [blame] | 270 | } |
Mathias Agopian | da27af9 | 2012-09-13 18:17:13 -0700 | [diff] [blame] | 271 | } |
| 272 | |
| 273 | void DisplayDevice::onSwapBuffersCompleted(HWComposer& hwc) const { |
| 274 | if (hwc.initCheck() == NO_ERROR) { |
Jesse Hall | 851cfe8 | 2013-03-20 13:44:00 -0700 | [diff] [blame] | 275 | mDisplaySurface->onFrameCommitted(); |
Mathias Agopian | da27af9 | 2012-09-13 18:17:13 -0700 | [diff] [blame] | 276 | } |
| 277 | } |
| 278 | |
Mathias Agopian | 0f2f5ff | 2012-07-31 23:09:07 -0700 | [diff] [blame] | 279 | uint32_t DisplayDevice::getFlags() const |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 280 | { |
| 281 | return mFlags; |
| 282 | } |
| 283 | |
Mathias Agopian | 875d8e1 | 2013-06-07 15:35:48 -0700 | [diff] [blame] | 284 | EGLBoolean DisplayDevice::makeCurrent(EGLDisplay dpy, EGLContext ctx) const { |
Mathias Agopian | da8d0a5 | 2012-09-04 15:05:38 -0700 | [diff] [blame] | 285 | EGLBoolean result = EGL_TRUE; |
Mathias Agopian | 52bbb1a | 2012-07-31 19:01:53 -0700 | [diff] [blame] | 286 | EGLSurface sur = eglGetCurrentSurface(EGL_DRAW); |
Mathias Agopian | 875d8e1 | 2013-06-07 15:35:48 -0700 | [diff] [blame] | 287 | if (sur != mSurface) { |
| 288 | result = eglMakeCurrent(dpy, mSurface, mSurface, ctx); |
Mathias Agopian | da8d0a5 | 2012-09-04 15:05:38 -0700 | [diff] [blame] | 289 | if (result == EGL_TRUE) { |
Jesse Hall | f460f55 | 2013-08-06 17:08:53 -0700 | [diff] [blame] | 290 | if (mType >= DisplayDevice::DISPLAY_VIRTUAL) |
| 291 | eglSwapInterval(dpy, 0); |
Mathias Agopian | da8d0a5 | 2012-09-04 15:05:38 -0700 | [diff] [blame] | 292 | } |
Mathias Agopian | 52bbb1a | 2012-07-31 19:01:53 -0700 | [diff] [blame] | 293 | } |
Mathias Agopian | 931bda1 | 2013-08-28 18:11:46 -0700 | [diff] [blame] | 294 | setViewportAndProjection(); |
Mathias Agopian | da8d0a5 | 2012-09-04 15:05:38 -0700 | [diff] [blame] | 295 | return result; |
Mathias Agopian | 52bbb1a | 2012-07-31 19:01:53 -0700 | [diff] [blame] | 296 | } |
| 297 | |
Mathias Agopian | 875d8e1 | 2013-06-07 15:35:48 -0700 | [diff] [blame] | 298 | void DisplayDevice::setViewportAndProjection() const { |
| 299 | size_t w = mDisplayWidth; |
| 300 | size_t h = mDisplayHeight; |
Dan Stoza | c187900 | 2014-05-22 15:59:05 -0700 | [diff] [blame] | 301 | Rect sourceCrop(0, 0, w, h); |
Riley Andrews | c3ebe66 | 2014-09-04 16:20:31 -0700 | [diff] [blame] | 302 | mFlinger->getRenderEngine().setViewportAndProjection(w, h, sourceCrop, h, |
| 303 | false, Transform::ROT_0); |
Mathias Agopian | bae92d0 | 2012-09-28 01:00:47 -0700 | [diff] [blame] | 304 | } |
| 305 | |
Mathias Agopian | 1b03149 | 2012-06-20 17:51:20 -0700 | [diff] [blame] | 306 | // ---------------------------------------------------------------------------- |
| 307 | |
Mathias Agopian | 13127d8 | 2013-03-05 17:47:11 -0800 | [diff] [blame] | 308 | void DisplayDevice::setVisibleLayersSortedByZ(const Vector< sp<Layer> >& layers) { |
Mathias Agopian | 3b1d2b6 | 2012-07-11 13:48:17 -0700 | [diff] [blame] | 309 | mVisibleLayersSortedByZ = layers; |
Mathias Agopian | ef7b9c7 | 2012-08-10 15:22:19 -0700 | [diff] [blame] | 310 | mSecureLayerVisible = false; |
Mathias Agopian | 3b1d2b6 | 2012-07-11 13:48:17 -0700 | [diff] [blame] | 311 | size_t count = layers.size(); |
| 312 | for (size_t i=0 ; i<count ; i++) { |
Mathias Agopian | 13127d8 | 2013-03-05 17:47:11 -0800 | [diff] [blame] | 313 | const sp<Layer>& layer(layers[i]); |
Mathias Agopian | f5f714a | 2013-02-26 16:54:05 -0800 | [diff] [blame] | 314 | if (layer->isSecure()) { |
Mathias Agopian | 3b1d2b6 | 2012-07-11 13:48:17 -0700 | [diff] [blame] | 315 | mSecureLayerVisible = true; |
| 316 | } |
| 317 | } |
| 318 | } |
| 319 | |
Mathias Agopian | 13127d8 | 2013-03-05 17:47:11 -0800 | [diff] [blame] | 320 | const Vector< sp<Layer> >& DisplayDevice::getVisibleLayersSortedByZ() const { |
Mathias Agopian | 3b1d2b6 | 2012-07-11 13:48:17 -0700 | [diff] [blame] | 321 | return mVisibleLayersSortedByZ; |
| 322 | } |
| 323 | |
Mathias Agopian | 0f2f5ff | 2012-07-31 23:09:07 -0700 | [diff] [blame] | 324 | bool DisplayDevice::getSecureLayerVisible() const { |
Mathias Agopian | 3b1d2b6 | 2012-07-11 13:48:17 -0700 | [diff] [blame] | 325 | return mSecureLayerVisible; |
| 326 | } |
| 327 | |
Mathias Agopian | cd60f99 | 2012-08-16 16:28:27 -0700 | [diff] [blame] | 328 | Region DisplayDevice::getDirtyRegion(bool repaintEverything) const { |
| 329 | Region dirty; |
Mathias Agopian | cd60f99 | 2012-08-16 16:28:27 -0700 | [diff] [blame] | 330 | if (repaintEverything) { |
| 331 | dirty.set(getBounds()); |
| 332 | } else { |
Mathias Agopian | da8d0a5 | 2012-09-04 15:05:38 -0700 | [diff] [blame] | 333 | const Transform& planeTransform(mGlobalTransform); |
Mathias Agopian | cd60f99 | 2012-08-16 16:28:27 -0700 | [diff] [blame] | 334 | dirty = planeTransform.transform(this->dirtyRegion); |
| 335 | dirty.andSelf(getBounds()); |
| 336 | } |
| 337 | return dirty; |
| 338 | } |
| 339 | |
Mathias Agopian | 3b1d2b6 | 2012-07-11 13:48:17 -0700 | [diff] [blame] | 340 | // ---------------------------------------------------------------------------- |
Prashant Malani | 2c9b11f | 2014-05-25 01:36:31 -0700 | [diff] [blame] | 341 | void DisplayDevice::setPowerMode(int mode) { |
| 342 | mPowerMode = mode; |
Mathias Agopian | d3ee231 | 2012-08-02 14:01:42 -0700 | [diff] [blame] | 343 | } |
| 344 | |
Prashant Malani | 2c9b11f | 2014-05-25 01:36:31 -0700 | [diff] [blame] | 345 | int DisplayDevice::getPowerMode() const { |
| 346 | return mPowerMode; |
Mathias Agopian | d3ee231 | 2012-08-02 14:01:42 -0700 | [diff] [blame] | 347 | } |
| 348 | |
Prashant Malani | 2c9b11f | 2014-05-25 01:36:31 -0700 | [diff] [blame] | 349 | bool DisplayDevice::isDisplayOn() const { |
| 350 | return (mPowerMode != HWC_POWER_MODE_OFF); |
Mathias Agopian | d3ee231 | 2012-08-02 14:01:42 -0700 | [diff] [blame] | 351 | } |
| 352 | |
| 353 | // ---------------------------------------------------------------------------- |
Michael Lentine | 6c9e34a | 2014-07-14 13:48:55 -0700 | [diff] [blame] | 354 | void DisplayDevice::setActiveConfig(int mode) { |
| 355 | mActiveConfig = mode; |
| 356 | } |
| 357 | |
| 358 | int DisplayDevice::getActiveConfig() const { |
| 359 | return mActiveConfig; |
| 360 | } |
| 361 | |
| 362 | // ---------------------------------------------------------------------------- |
Mathias Agopian | d3ee231 | 2012-08-02 14:01:42 -0700 | [diff] [blame] | 363 | |
Mathias Agopian | 28947d7 | 2012-08-08 18:51:15 -0700 | [diff] [blame] | 364 | void DisplayDevice::setLayerStack(uint32_t stack) { |
| 365 | mLayerStack = stack; |
| 366 | dirtyRegion.set(bounds()); |
| 367 | } |
| 368 | |
| 369 | // ---------------------------------------------------------------------------- |
| 370 | |
Mathias Agopian | c1c05de | 2013-09-17 23:45:22 -0700 | [diff] [blame] | 371 | uint32_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 Agopian | 0f2f5ff | 2012-07-31 23:09:07 -0700 | [diff] [blame] | 390 | status_t DisplayDevice::orientationToTransfrom( |
Mathias Agopian | 1b03149 | 2012-06-20 17:51:20 -0700 | [diff] [blame] | 391 | int orientation, int w, int h, Transform* tr) |
| 392 | { |
| 393 | uint32_t flags = 0; |
| 394 | switch (orientation) { |
Mathias Agopian | 3165cc2 | 2012-08-08 19:42:09 -0700 | [diff] [blame] | 395 | case DisplayState::eOrientationDefault: |
Mathias Agopian | 1b03149 | 2012-06-20 17:51:20 -0700 | [diff] [blame] | 396 | flags = Transform::ROT_0; |
| 397 | break; |
Mathias Agopian | 3165cc2 | 2012-08-08 19:42:09 -0700 | [diff] [blame] | 398 | case DisplayState::eOrientation90: |
Mathias Agopian | 1b03149 | 2012-06-20 17:51:20 -0700 | [diff] [blame] | 399 | flags = Transform::ROT_90; |
| 400 | break; |
Mathias Agopian | 3165cc2 | 2012-08-08 19:42:09 -0700 | [diff] [blame] | 401 | case DisplayState::eOrientation180: |
Mathias Agopian | 1b03149 | 2012-06-20 17:51:20 -0700 | [diff] [blame] | 402 | flags = Transform::ROT_180; |
| 403 | break; |
Mathias Agopian | 3165cc2 | 2012-08-08 19:42:09 -0700 | [diff] [blame] | 404 | case DisplayState::eOrientation270: |
Mathias Agopian | 1b03149 | 2012-06-20 17:51:20 -0700 | [diff] [blame] | 405 | 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 Lentine | 47e4540 | 2014-07-18 15:34:25 -0700 | [diff] [blame] | 414 | void DisplayDevice::setDisplaySize(const int newWidth, const int newHeight) { |
| 415 | dirtyRegion.set(getBounds()); |
| 416 | |
Michael Lentine | f2568de | 2014-08-20 10:51:23 -0700 | [diff] [blame] | 417 | if (mSurface != EGL_NO_SURFACE) { |
| 418 | eglDestroySurface(mDisplay, mSurface); |
| 419 | mSurface = EGL_NO_SURFACE; |
| 420 | } |
| 421 | |
Michael Lentine | 47e4540 | 2014-07-18 15:34:25 -0700 | [diff] [blame] | 422 | 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 Agopian | 00e8c7a | 2012-09-04 19:30:46 -0700 | [diff] [blame] | 435 | void DisplayDevice::setProjection(int orientation, |
Mathias Agopian | f5f714a | 2013-02-26 16:54:05 -0800 | [diff] [blame] | 436 | 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 Agopian | 6c7f25a | 2013-05-09 20:37:10 -0700 | [diff] [blame] | 496 | mScissor = getBounds(); |
Mathias Agopian | f5f714a | 2013-02-26 16:54:05 -0800 | [diff] [blame] | 497 | } |
| 498 | |
Mathias Agopian | da8d0a5 | 2012-09-04 15:05:38 -0700 | [diff] [blame] | 499 | mOrientation = orientation; |
Mathias Agopian | 00e8c7a | 2012-09-04 19:30:46 -0700 | [diff] [blame] | 500 | mViewport = viewport; |
| 501 | mFrame = frame; |
Mathias Agopian | 1b03149 | 2012-06-20 17:51:20 -0700 | [diff] [blame] | 502 | } |
Mathias Agopian | 1d12d8a | 2012-09-18 01:38:00 -0700 | [diff] [blame] | 503 | |
Mathias Agopian | 74d211a | 2013-04-22 16:55:35 +0200 | [diff] [blame] | 504 | void DisplayDevice::dump(String8& result) const { |
Mathias Agopian | 1d12d8a | 2012-09-18 01:38:00 -0700 | [diff] [blame] | 505 | const Transform& tr(mGlobalTransform); |
Mathias Agopian | 74d211a | 2013-04-22 16:55:35 +0200 | [diff] [blame] | 506 | result.appendFormat( |
Mathias Agopian | 1d12d8a | 2012-09-18 01:38:00 -0700 | [diff] [blame] | 507 | "+ DisplayDevice: %s\n" |
Jesse Hall | 02d8656 | 2013-03-25 14:43:23 -0700 | [diff] [blame] | 508 | " type=%x, hwcId=%d, layerStack=%u, (%4dx%4d), ANativeWindow=%p, orient=%2d (type=%08x), " |
Michael Lentine | 6c9e34a | 2014-07-14 13:48:55 -0700 | [diff] [blame] | 509 | "flips=%u, isSecure=%d, secureVis=%d, powerMode=%d, activeConfig=%d, numLayers=%zu\n" |
Mathias Agopian | 766dc49 | 2012-10-30 18:08:06 -0700 | [diff] [blame] | 510 | " v:[%d,%d,%d,%d], f:[%d,%d,%d,%d], s:[%d,%d,%d,%d]," |
Mathias Agopian | 1d12d8a | 2012-09-18 01:38:00 -0700 | [diff] [blame] | 511 | "transform:[[%0.3f,%0.3f,%0.3f][%0.3f,%0.3f,%0.3f][%0.3f,%0.3f,%0.3f]]\n", |
Jesse Hall | 02d8656 | 2013-03-25 14:43:23 -0700 | [diff] [blame] | 512 | mDisplayName.string(), mType, mHwcDisplayId, |
Mathias Agopian | 1d12d8a | 2012-09-18 01:38:00 -0700 | [diff] [blame] | 513 | mLayerStack, mDisplayWidth, mDisplayHeight, mNativeWindow.get(), |
| 514 | mOrientation, tr.getType(), getPageFlipCount(), |
Michael Lentine | 6c9e34a | 2014-07-14 13:48:55 -0700 | [diff] [blame] | 515 | mIsSecure, mSecureLayerVisible, mPowerMode, mActiveConfig, |
| 516 | mVisibleLayersSortedByZ.size(), |
Mathias Agopian | 1d12d8a | 2012-09-18 01:38:00 -0700 | [diff] [blame] | 517 | mViewport.left, mViewport.top, mViewport.right, mViewport.bottom, |
| 518 | mFrame.left, mFrame.top, mFrame.right, mFrame.bottom, |
Mathias Agopian | 766dc49 | 2012-10-30 18:08:06 -0700 | [diff] [blame] | 519 | mScissor.left, mScissor.top, mScissor.right, mScissor.bottom, |
Mathias Agopian | 1d12d8a | 2012-09-18 01:38:00 -0700 | [diff] [blame] | 520 | 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 Hall | 99c7dbb | 2013-03-14 14:29:29 -0700 | [diff] [blame] | 524 | String8 surfaceDump; |
Dan Stoza | f10c46e | 2014-11-11 10:32:31 -0800 | [diff] [blame] | 525 | mDisplaySurface->dumpAsString(surfaceDump); |
Jesse Hall | 99c7dbb | 2013-03-14 14:29:29 -0700 | [diff] [blame] | 526 | result.append(surfaceDump); |
Mathias Agopian | 1d12d8a | 2012-09-18 01:38:00 -0700 | [diff] [blame] | 527 | } |