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 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 32 | #include <GLES/gl.h> |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 33 | #include <EGL/egl.h> |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 34 | #include <EGL/eglext.h> |
| 35 | |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 36 | #include <hardware/gralloc.h> |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 37 | |
Jesse Hall | 99c7dbb | 2013-03-14 14:29:29 -0700 | [diff] [blame] | 38 | #include "DisplayHardware/DisplaySurface.h" |
Mathias Agopian | 1b03149 | 2012-06-20 17:51:20 -0700 | [diff] [blame] | 39 | #include "DisplayHardware/HWComposer.h" |
| 40 | |
Mathias Agopian | da8d0a5 | 2012-09-04 15:05:38 -0700 | [diff] [blame] | 41 | #include "clz.h" |
Mathias Agopian | 0f2f5ff | 2012-07-31 23:09:07 -0700 | [diff] [blame] | 42 | #include "DisplayDevice.h" |
Mathias Agopian | 1f7bec6 | 2010-06-25 18:02:21 -0700 | [diff] [blame] | 43 | #include "GLExtensions.h" |
Mathias Agopian | c7d14e2 | 2011-08-01 16:32:21 -0700 | [diff] [blame] | 44 | #include "SurfaceFlinger.h" |
Mathias Agopian | 13127d8 | 2013-03-05 17:47:11 -0800 | [diff] [blame] | 45 | #include "Layer.h" |
Mathias Agopian | 1f7bec6 | 2010-06-25 18:02:21 -0700 | [diff] [blame] | 46 | |
Mathias Agopian | a491260 | 2012-07-12 14:25:33 -0700 | [diff] [blame] | 47 | // ---------------------------------------------------------------------------- |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 48 | using namespace android; |
Mathias Agopian | a491260 | 2012-07-12 14:25:33 -0700 | [diff] [blame] | 49 | // ---------------------------------------------------------------------------- |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 50 | |
| 51 | static __attribute__((noinline)) |
| 52 | void checkGLErrors() |
| 53 | { |
Mathias Agopian | cbb288b | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 54 | do { |
| 55 | // there could be more than one error flag |
| 56 | GLenum error = glGetError(); |
| 57 | if (error == GL_NO_ERROR) |
| 58 | break; |
Steve Block | e6f43dd | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 59 | ALOGE("GL error 0x%04x", int(error)); |
Mathias Agopian | cbb288b | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 60 | } while(true); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 61 | } |
| 62 | |
Mathias Agopian | a491260 | 2012-07-12 14:25:33 -0700 | [diff] [blame] | 63 | // ---------------------------------------------------------------------------- |
| 64 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 65 | /* |
| 66 | * Initialize the display to the specified values. |
| 67 | * |
| 68 | */ |
| 69 | |
Mathias Agopian | 0f2f5ff | 2012-07-31 23:09:07 -0700 | [diff] [blame] | 70 | DisplayDevice::DisplayDevice( |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 71 | const sp<SurfaceFlinger>& flinger, |
Jamie Gennis | dd3cb84 | 2012-10-19 18:19:11 -0700 | [diff] [blame] | 72 | DisplayType type, |
| 73 | bool isSecure, |
| 74 | const wp<IBinder>& displayToken, |
Jesse Hall | 99c7dbb | 2013-03-14 14:29:29 -0700 | [diff] [blame] | 75 | const sp<DisplaySurface>& displaySurface, |
Mathias Agopian | a491260 | 2012-07-12 14:25:33 -0700 | [diff] [blame] | 76 | EGLConfig config) |
Mathias Agopian | 92a979a | 2012-08-02 18:32:23 -0700 | [diff] [blame] | 77 | : mFlinger(flinger), |
Mathias Agopian | 3ee454a | 2012-08-27 16:28:24 -0700 | [diff] [blame] | 78 | mType(type), mHwcDisplayId(-1), |
Chih-Wei Huang | 27e2562 | 2013-01-07 17:33:56 +0800 | [diff] [blame] | 79 | mDisplayToken(displayToken), |
Jesse Hall | 99c7dbb | 2013-03-14 14:29:29 -0700 | [diff] [blame] | 80 | mDisplaySurface(displaySurface), |
Mathias Agopian | 92a979a | 2012-08-02 18:32:23 -0700 | [diff] [blame] | 81 | mDisplay(EGL_NO_DISPLAY), |
| 82 | mSurface(EGL_NO_SURFACE), |
| 83 | mContext(EGL_NO_CONTEXT), |
Mathias Agopian | 92a979a | 2012-08-02 18:32:23 -0700 | [diff] [blame] | 84 | mDisplayWidth(), mDisplayHeight(), mFormat(), |
| 85 | mFlags(), |
| 86 | mPageFlipCount(), |
Jamie Gennis | dd3cb84 | 2012-10-19 18:19:11 -0700 | [diff] [blame] | 87 | mIsSecure(isSecure), |
Mathias Agopian | 92a979a | 2012-08-02 18:32:23 -0700 | [diff] [blame] | 88 | mSecureLayerVisible(false), |
| 89 | mScreenAcquired(false), |
Jesse Hall | 01e2905 | 2013-02-19 16:13:35 -0800 | [diff] [blame] | 90 | mLayerStack(NO_LAYER_STACK), |
Mathias Agopian | da8d0a5 | 2012-09-04 15:05:38 -0700 | [diff] [blame] | 91 | mOrientation() |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 92 | { |
Jesse Hall | 99c7dbb | 2013-03-14 14:29:29 -0700 | [diff] [blame] | 93 | mNativeWindow = new Surface(mDisplaySurface->getIGraphicBufferProducer()); |
Mathias Agopian | a491260 | 2012-07-12 14:25:33 -0700 | [diff] [blame] | 94 | init(config); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 95 | } |
| 96 | |
Mathias Agopian | 0f2f5ff | 2012-07-31 23:09:07 -0700 | [diff] [blame] | 97 | DisplayDevice::~DisplayDevice() { |
Mathias Agopian | 92a979a | 2012-08-02 18:32:23 -0700 | [diff] [blame] | 98 | if (mSurface != EGL_NO_SURFACE) { |
| 99 | eglDestroySurface(mDisplay, mSurface); |
| 100 | mSurface = EGL_NO_SURFACE; |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | bool DisplayDevice::isValid() const { |
| 105 | return mFlinger != NULL; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 106 | } |
| 107 | |
Mathias Agopian | 0f2f5ff | 2012-07-31 23:09:07 -0700 | [diff] [blame] | 108 | int DisplayDevice::getWidth() const { |
Mathias Agopian | a491260 | 2012-07-12 14:25:33 -0700 | [diff] [blame] | 109 | return mDisplayWidth; |
| 110 | } |
| 111 | |
Mathias Agopian | 0f2f5ff | 2012-07-31 23:09:07 -0700 | [diff] [blame] | 112 | int DisplayDevice::getHeight() const { |
Mathias Agopian | a491260 | 2012-07-12 14:25:33 -0700 | [diff] [blame] | 113 | return mDisplayHeight; |
| 114 | } |
| 115 | |
Mathias Agopian | 0f2f5ff | 2012-07-31 23:09:07 -0700 | [diff] [blame] | 116 | PixelFormat DisplayDevice::getFormat() const { |
Mathias Agopian | a491260 | 2012-07-12 14:25:33 -0700 | [diff] [blame] | 117 | return mFormat; |
| 118 | } |
| 119 | |
Mathias Agopian | 0f2f5ff | 2012-07-31 23:09:07 -0700 | [diff] [blame] | 120 | EGLSurface DisplayDevice::getEGLSurface() const { |
Mathias Agopian | a491260 | 2012-07-12 14:25:33 -0700 | [diff] [blame] | 121 | return mSurface; |
| 122 | } |
| 123 | |
Mathias Agopian | 0f2f5ff | 2012-07-31 23:09:07 -0700 | [diff] [blame] | 124 | void DisplayDevice::init(EGLConfig config) |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 125 | { |
Mathias Agopian | a491260 | 2012-07-12 14:25:33 -0700 | [diff] [blame] | 126 | ANativeWindow* const window = mNativeWindow.get(); |
| 127 | |
Mathias Agopian | 6163091 | 2011-07-06 16:35:30 -0700 | [diff] [blame] | 128 | int format; |
Mathias Agopian | 6163091 | 2011-07-06 16:35:30 -0700 | [diff] [blame] | 129 | window->query(window, NATIVE_WINDOW_FORMAT, &format); |
Mathias Agopian | b5dd9c0 | 2012-03-22 12:15:54 -0700 | [diff] [blame] | 130 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 131 | /* |
Mathias Agopian | a491260 | 2012-07-12 14:25:33 -0700 | [diff] [blame] | 132 | * Create our display's surface |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 133 | */ |
| 134 | |
Mathias Agopian | a491260 | 2012-07-12 14:25:33 -0700 | [diff] [blame] | 135 | EGLSurface surface; |
| 136 | EGLint w, h; |
| 137 | EGLDisplay display = eglGetDisplay(EGL_DEFAULT_DISPLAY); |
| 138 | surface = eglCreateWindowSurface(display, config, window, NULL); |
Mathias Agopian | 1b03149 | 2012-06-20 17:51:20 -0700 | [diff] [blame] | 139 | eglQuerySurface(display, surface, EGL_WIDTH, &mDisplayWidth); |
| 140 | eglQuerySurface(display, surface, EGL_HEIGHT, &mDisplayHeight); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 141 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 142 | mDisplay = display; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 143 | mSurface = surface; |
Mathias Agopian | a491260 | 2012-07-12 14:25:33 -0700 | [diff] [blame] | 144 | mFormat = format; |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 145 | mPageFlipCount = 0; |
Mathias Agopian | da8d0a5 | 2012-09-04 15:05:38 -0700 | [diff] [blame] | 146 | mViewport.makeInvalid(); |
| 147 | mFrame.makeInvalid(); |
Mathias Agopian | 1f7bec6 | 2010-06-25 18:02:21 -0700 | [diff] [blame] | 148 | |
Mathias Agopian | 5f20e2d | 2012-08-10 18:50:38 -0700 | [diff] [blame] | 149 | // external displays are always considered enabled |
Mathias Agopian | 3ee454a | 2012-08-27 16:28:24 -0700 | [diff] [blame] | 150 | mScreenAcquired = (mType >= DisplayDevice::NUM_DISPLAY_TYPES); |
| 151 | |
| 152 | // get an h/w composer ID |
| 153 | mHwcDisplayId = mFlinger->allocateHwcDisplayId(mType); |
Mathias Agopian | 5f20e2d | 2012-08-10 18:50:38 -0700 | [diff] [blame] | 154 | |
Andy McFadden | 8dfa92f | 2012-09-17 18:27:17 -0700 | [diff] [blame] | 155 | // Name the display. The name will be replaced shortly if the display |
| 156 | // was created with createDisplay(). |
| 157 | switch (mType) { |
| 158 | case DISPLAY_PRIMARY: |
| 159 | mDisplayName = "Built-in Screen"; |
| 160 | break; |
| 161 | case DISPLAY_EXTERNAL: |
| 162 | mDisplayName = "HDMI Screen"; |
| 163 | break; |
| 164 | default: |
| 165 | mDisplayName = "Virtual Screen"; // e.g. Overlay #n |
| 166 | break; |
| 167 | } |
| 168 | |
Mathias Agopian | 98a121a | 2012-07-24 21:08:59 -0700 | [diff] [blame] | 169 | // initialize the display orientation transform. |
Mathias Agopian | 00e8c7a | 2012-09-04 19:30:46 -0700 | [diff] [blame] | 170 | setProjection(DisplayState::eOrientationDefault, mViewport, mFrame); |
Mathias Agopian | a350ff9 | 2010-08-10 17:14:02 -0700 | [diff] [blame] | 171 | } |
| 172 | |
Mathias Agopian | 9e2463e | 2012-09-21 18:26:16 -0700 | [diff] [blame] | 173 | void DisplayDevice::setDisplayName(const String8& displayName) { |
| 174 | if (!displayName.isEmpty()) { |
| 175 | // never override the name with an empty name |
| 176 | mDisplayName = displayName; |
| 177 | } |
| 178 | } |
| 179 | |
Mathias Agopian | 0f2f5ff | 2012-07-31 23:09:07 -0700 | [diff] [blame] | 180 | uint32_t DisplayDevice::getPageFlipCount() const { |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 181 | return mPageFlipCount; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 182 | } |
| 183 | |
Mathias Agopian | 0f2f5ff | 2012-07-31 23:09:07 -0700 | [diff] [blame] | 184 | status_t DisplayDevice::compositionComplete() const { |
Jesse Hall | 99c7dbb | 2013-03-14 14:29:29 -0700 | [diff] [blame] | 185 | return mDisplaySurface->compositionComplete(); |
Mathias Agopian | 74faca2 | 2009-09-17 16:18:16 -0700 | [diff] [blame] | 186 | } |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 187 | |
Mathias Agopian | 0f2f5ff | 2012-07-31 23:09:07 -0700 | [diff] [blame] | 188 | void DisplayDevice::flip(const Region& dirty) const |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 189 | { |
| 190 | checkGLErrors(); |
| 191 | |
| 192 | EGLDisplay dpy = mDisplay; |
| 193 | EGLSurface surface = mSurface; |
| 194 | |
Jesse Hall | 01e2905 | 2013-02-19 16:13:35 -0800 | [diff] [blame] | 195 | #ifdef EGL_ANDROID_swap_rectangle |
Mathias Agopian | df3ca30 | 2009-05-04 19:29:25 -0700 | [diff] [blame] | 196 | if (mFlags & SWAP_RECTANGLE) { |
Mathias Agopian | b8a5560 | 2009-06-26 19:06:36 -0700 | [diff] [blame] | 197 | const Region newDirty(dirty.intersect(bounds())); |
| 198 | const Rect b(newDirty.getBounds()); |
Mathias Agopian | df3ca30 | 2009-05-04 19:29:25 -0700 | [diff] [blame] | 199 | eglSetSwapRectangleANDROID(dpy, surface, |
| 200 | b.left, b.top, b.width(), b.height()); |
Jesse Hall | 01e2905 | 2013-02-19 16:13:35 -0800 | [diff] [blame] | 201 | } |
Mathias Agopian | 5e78e09 | 2009-06-11 17:19:54 -0700 | [diff] [blame] | 202 | #endif |
Mathias Agopian | d870703 | 2012-09-18 01:21:55 -0700 | [diff] [blame] | 203 | |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 204 | mPageFlipCount++; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 205 | } |
| 206 | |
Mathias Agopian | da27af9 | 2012-09-13 18:17:13 -0700 | [diff] [blame] | 207 | void DisplayDevice::swapBuffers(HWComposer& hwc) const { |
Jesse Hall | 99c7dbb | 2013-03-14 14:29:29 -0700 | [diff] [blame] | 208 | // We need to call eglSwapBuffers() unless: |
| 209 | // (a) there was no GLES composition this frame, or |
| 210 | // (b) we're using a legacy HWC with no framebuffer target support (in |
| 211 | // which case HWComposer::commit() handles things). |
| 212 | if (hwc.initCheck() != NO_ERROR || |
| 213 | (hwc.hasGlesComposition(mHwcDisplayId) && |
| 214 | hwc.supportsFramebufferTarget())) { |
| 215 | EGLBoolean success = eglSwapBuffers(mDisplay, mSurface); |
| 216 | if (!success) { |
| 217 | EGLint error = eglGetError(); |
| 218 | if (error == EGL_CONTEXT_LOST || |
| 219 | mType == DisplayDevice::DISPLAY_PRIMARY) { |
| 220 | LOG_ALWAYS_FATAL("eglSwapBuffers(%p, %p) failed with 0x%08x", |
| 221 | mDisplay, mSurface, error); |
| 222 | } else { |
| 223 | ALOGE("eglSwapBuffers(%p, %p) failed with 0x%08x", |
| 224 | mDisplay, mSurface, error); |
Mathias Agopian | da27af9 | 2012-09-13 18:17:13 -0700 | [diff] [blame] | 225 | } |
| 226 | } |
| 227 | } |
Mathias Agopian | 52e2148 | 2012-09-24 18:07:21 -0700 | [diff] [blame] | 228 | |
Jesse Hall | 99c7dbb | 2013-03-14 14:29:29 -0700 | [diff] [blame] | 229 | status_t result = mDisplaySurface->advanceFrame(); |
| 230 | if (result != NO_ERROR) { |
| 231 | ALOGE("[%s] failed pushing new frame to HWC: %d", |
| 232 | mDisplayName.string(), result); |
Mathias Agopian | 3234138 | 2012-09-25 19:16:28 -0700 | [diff] [blame] | 233 | } |
Mathias Agopian | da27af9 | 2012-09-13 18:17:13 -0700 | [diff] [blame] | 234 | } |
| 235 | |
| 236 | void DisplayDevice::onSwapBuffersCompleted(HWComposer& hwc) const { |
| 237 | if (hwc.initCheck() == NO_ERROR) { |
Jesse Hall | 851cfe8 | 2013-03-20 13:44:00 -0700 | [diff] [blame] | 238 | mDisplaySurface->onFrameCommitted(); |
Mathias Agopian | da27af9 | 2012-09-13 18:17:13 -0700 | [diff] [blame] | 239 | } |
| 240 | } |
| 241 | |
Mathias Agopian | 0f2f5ff | 2012-07-31 23:09:07 -0700 | [diff] [blame] | 242 | uint32_t DisplayDevice::getFlags() const |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 243 | { |
| 244 | return mFlags; |
| 245 | } |
| 246 | |
Mathias Agopian | da8d0a5 | 2012-09-04 15:05:38 -0700 | [diff] [blame] | 247 | EGLBoolean DisplayDevice::makeCurrent(EGLDisplay dpy, |
| 248 | const sp<const DisplayDevice>& hw, EGLContext ctx) { |
| 249 | EGLBoolean result = EGL_TRUE; |
Mathias Agopian | 52bbb1a | 2012-07-31 19:01:53 -0700 | [diff] [blame] | 250 | EGLSurface sur = eglGetCurrentSurface(EGL_DRAW); |
Mathias Agopian | 4297734 | 2012-08-05 00:40:46 -0700 | [diff] [blame] | 251 | if (sur != hw->mSurface) { |
Mathias Agopian | da8d0a5 | 2012-09-04 15:05:38 -0700 | [diff] [blame] | 252 | result = eglMakeCurrent(dpy, hw->mSurface, hw->mSurface, ctx); |
| 253 | if (result == EGL_TRUE) { |
Mathias Agopian | bae92d0 | 2012-09-28 01:00:47 -0700 | [diff] [blame] | 254 | setViewportAndProjection(hw); |
Mathias Agopian | da8d0a5 | 2012-09-04 15:05:38 -0700 | [diff] [blame] | 255 | } |
Mathias Agopian | 52bbb1a | 2012-07-31 19:01:53 -0700 | [diff] [blame] | 256 | } |
Mathias Agopian | da8d0a5 | 2012-09-04 15:05:38 -0700 | [diff] [blame] | 257 | return result; |
Mathias Agopian | 52bbb1a | 2012-07-31 19:01:53 -0700 | [diff] [blame] | 258 | } |
| 259 | |
Mathias Agopian | bae92d0 | 2012-09-28 01:00:47 -0700 | [diff] [blame] | 260 | void DisplayDevice::setViewportAndProjection(const sp<const DisplayDevice>& hw) { |
| 261 | GLsizei w = hw->mDisplayWidth; |
| 262 | GLsizei h = hw->mDisplayHeight; |
| 263 | glViewport(0, 0, w, h); |
| 264 | glMatrixMode(GL_PROJECTION); |
| 265 | glLoadIdentity(); |
| 266 | // put the origin in the left-bottom corner |
| 267 | glOrthof(0, w, 0, h, 0, 1); // l=0, r=w ; b=0, t=h |
Mathias Agopian | 135e589 | 2012-09-30 16:43:20 -0700 | [diff] [blame] | 268 | glMatrixMode(GL_MODELVIEW); |
Mathias Agopian | bae92d0 | 2012-09-28 01:00:47 -0700 | [diff] [blame] | 269 | } |
| 270 | |
Mathias Agopian | 1b03149 | 2012-06-20 17:51:20 -0700 | [diff] [blame] | 271 | // ---------------------------------------------------------------------------- |
| 272 | |
Mathias Agopian | 13127d8 | 2013-03-05 17:47:11 -0800 | [diff] [blame] | 273 | void DisplayDevice::setVisibleLayersSortedByZ(const Vector< sp<Layer> >& layers) { |
Mathias Agopian | 3b1d2b6 | 2012-07-11 13:48:17 -0700 | [diff] [blame] | 274 | mVisibleLayersSortedByZ = layers; |
Mathias Agopian | ef7b9c7 | 2012-08-10 15:22:19 -0700 | [diff] [blame] | 275 | mSecureLayerVisible = false; |
Mathias Agopian | 3b1d2b6 | 2012-07-11 13:48:17 -0700 | [diff] [blame] | 276 | size_t count = layers.size(); |
| 277 | for (size_t i=0 ; i<count ; i++) { |
Mathias Agopian | 13127d8 | 2013-03-05 17:47:11 -0800 | [diff] [blame] | 278 | const sp<Layer>& layer(layers[i]); |
Mathias Agopian | f5f714a | 2013-02-26 16:54:05 -0800 | [diff] [blame] | 279 | if (layer->isSecure()) { |
Mathias Agopian | 3b1d2b6 | 2012-07-11 13:48:17 -0700 | [diff] [blame] | 280 | mSecureLayerVisible = true; |
| 281 | } |
| 282 | } |
| 283 | } |
| 284 | |
Mathias Agopian | 13127d8 | 2013-03-05 17:47:11 -0800 | [diff] [blame] | 285 | const Vector< sp<Layer> >& DisplayDevice::getVisibleLayersSortedByZ() const { |
Mathias Agopian | 3b1d2b6 | 2012-07-11 13:48:17 -0700 | [diff] [blame] | 286 | return mVisibleLayersSortedByZ; |
| 287 | } |
| 288 | |
Mathias Agopian | 0f2f5ff | 2012-07-31 23:09:07 -0700 | [diff] [blame] | 289 | bool DisplayDevice::getSecureLayerVisible() const { |
Mathias Agopian | 3b1d2b6 | 2012-07-11 13:48:17 -0700 | [diff] [blame] | 290 | return mSecureLayerVisible; |
| 291 | } |
| 292 | |
Mathias Agopian | cd60f99 | 2012-08-16 16:28:27 -0700 | [diff] [blame] | 293 | Region DisplayDevice::getDirtyRegion(bool repaintEverything) const { |
| 294 | Region dirty; |
Mathias Agopian | cd60f99 | 2012-08-16 16:28:27 -0700 | [diff] [blame] | 295 | if (repaintEverything) { |
| 296 | dirty.set(getBounds()); |
| 297 | } else { |
Mathias Agopian | da8d0a5 | 2012-09-04 15:05:38 -0700 | [diff] [blame] | 298 | const Transform& planeTransform(mGlobalTransform); |
Mathias Agopian | cd60f99 | 2012-08-16 16:28:27 -0700 | [diff] [blame] | 299 | dirty = planeTransform.transform(this->dirtyRegion); |
| 300 | dirty.andSelf(getBounds()); |
| 301 | } |
| 302 | return dirty; |
| 303 | } |
| 304 | |
Mathias Agopian | 3b1d2b6 | 2012-07-11 13:48:17 -0700 | [diff] [blame] | 305 | // ---------------------------------------------------------------------------- |
| 306 | |
Mathias Agopian | d3ee231 | 2012-08-02 14:01:42 -0700 | [diff] [blame] | 307 | bool DisplayDevice::canDraw() const { |
| 308 | return mScreenAcquired; |
| 309 | } |
| 310 | |
| 311 | void DisplayDevice::releaseScreen() const { |
| 312 | mScreenAcquired = false; |
| 313 | } |
| 314 | |
| 315 | void DisplayDevice::acquireScreen() const { |
| 316 | mScreenAcquired = true; |
| 317 | } |
| 318 | |
| 319 | bool DisplayDevice::isScreenAcquired() const { |
| 320 | return mScreenAcquired; |
| 321 | } |
| 322 | |
| 323 | // ---------------------------------------------------------------------------- |
| 324 | |
Mathias Agopian | 28947d7 | 2012-08-08 18:51:15 -0700 | [diff] [blame] | 325 | void DisplayDevice::setLayerStack(uint32_t stack) { |
| 326 | mLayerStack = stack; |
| 327 | dirtyRegion.set(bounds()); |
| 328 | } |
| 329 | |
| 330 | // ---------------------------------------------------------------------------- |
| 331 | |
Mathias Agopian | 0f2f5ff | 2012-07-31 23:09:07 -0700 | [diff] [blame] | 332 | status_t DisplayDevice::orientationToTransfrom( |
Mathias Agopian | 1b03149 | 2012-06-20 17:51:20 -0700 | [diff] [blame] | 333 | int orientation, int w, int h, Transform* tr) |
| 334 | { |
| 335 | uint32_t flags = 0; |
| 336 | switch (orientation) { |
Mathias Agopian | 3165cc2 | 2012-08-08 19:42:09 -0700 | [diff] [blame] | 337 | case DisplayState::eOrientationDefault: |
Mathias Agopian | 1b03149 | 2012-06-20 17:51:20 -0700 | [diff] [blame] | 338 | flags = Transform::ROT_0; |
| 339 | break; |
Mathias Agopian | 3165cc2 | 2012-08-08 19:42:09 -0700 | [diff] [blame] | 340 | case DisplayState::eOrientation90: |
Mathias Agopian | 1b03149 | 2012-06-20 17:51:20 -0700 | [diff] [blame] | 341 | flags = Transform::ROT_90; |
| 342 | break; |
Mathias Agopian | 3165cc2 | 2012-08-08 19:42:09 -0700 | [diff] [blame] | 343 | case DisplayState::eOrientation180: |
Mathias Agopian | 1b03149 | 2012-06-20 17:51:20 -0700 | [diff] [blame] | 344 | flags = Transform::ROT_180; |
| 345 | break; |
Mathias Agopian | 3165cc2 | 2012-08-08 19:42:09 -0700 | [diff] [blame] | 346 | case DisplayState::eOrientation270: |
Mathias Agopian | 1b03149 | 2012-06-20 17:51:20 -0700 | [diff] [blame] | 347 | flags = Transform::ROT_270; |
| 348 | break; |
| 349 | default: |
| 350 | return BAD_VALUE; |
| 351 | } |
| 352 | tr->set(flags, w, h); |
| 353 | return NO_ERROR; |
| 354 | } |
| 355 | |
Mathias Agopian | 00e8c7a | 2012-09-04 19:30:46 -0700 | [diff] [blame] | 356 | void DisplayDevice::setProjection(int orientation, |
Mathias Agopian | f5f714a | 2013-02-26 16:54:05 -0800 | [diff] [blame] | 357 | const Rect& newViewport, const Rect& newFrame) { |
| 358 | Rect viewport(newViewport); |
| 359 | Rect frame(newFrame); |
| 360 | |
| 361 | const int w = mDisplayWidth; |
| 362 | const int h = mDisplayHeight; |
| 363 | |
| 364 | Transform R; |
| 365 | DisplayDevice::orientationToTransfrom(orientation, w, h, &R); |
| 366 | |
| 367 | if (!frame.isValid()) { |
| 368 | // the destination frame can be invalid if it has never been set, |
| 369 | // in that case we assume the whole display frame. |
| 370 | frame = Rect(w, h); |
| 371 | } |
| 372 | |
| 373 | if (viewport.isEmpty()) { |
| 374 | // viewport can be invalid if it has never been set, in that case |
| 375 | // we assume the whole display size. |
| 376 | // it's also invalid to have an empty viewport, so we handle that |
| 377 | // case in the same way. |
| 378 | viewport = Rect(w, h); |
| 379 | if (R.getOrientation() & Transform::ROT_90) { |
| 380 | // viewport is always specified in the logical orientation |
| 381 | // of the display (ie: post-rotation). |
| 382 | swap(viewport.right, viewport.bottom); |
| 383 | } |
| 384 | } |
| 385 | |
| 386 | dirtyRegion.set(getBounds()); |
| 387 | |
| 388 | Transform TL, TP, S; |
| 389 | float src_width = viewport.width(); |
| 390 | float src_height = viewport.height(); |
| 391 | float dst_width = frame.width(); |
| 392 | float dst_height = frame.height(); |
| 393 | if (src_width != dst_width || src_height != dst_height) { |
| 394 | float sx = dst_width / src_width; |
| 395 | float sy = dst_height / src_height; |
| 396 | S.set(sx, 0, 0, sy); |
| 397 | } |
| 398 | |
| 399 | float src_x = viewport.left; |
| 400 | float src_y = viewport.top; |
| 401 | float dst_x = frame.left; |
| 402 | float dst_y = frame.top; |
| 403 | TL.set(-src_x, -src_y); |
| 404 | TP.set(dst_x, dst_y); |
| 405 | |
| 406 | // The viewport and frame are both in the logical orientation. |
| 407 | // Apply the logical translation, scale to physical size, apply the |
| 408 | // physical translation and finally rotate to the physical orientation. |
| 409 | mGlobalTransform = R * TP * S * TL; |
| 410 | |
| 411 | const uint8_t type = mGlobalTransform.getType(); |
| 412 | mNeedsFiltering = (!mGlobalTransform.preserveRects() || |
| 413 | (type >= Transform::SCALE)); |
| 414 | |
| 415 | mScissor = mGlobalTransform.transform(viewport); |
| 416 | if (mScissor.isEmpty()) { |
| 417 | mScissor.set(getBounds()); |
| 418 | } |
| 419 | |
Mathias Agopian | da8d0a5 | 2012-09-04 15:05:38 -0700 | [diff] [blame] | 420 | mOrientation = orientation; |
Mathias Agopian | 00e8c7a | 2012-09-04 19:30:46 -0700 | [diff] [blame] | 421 | mViewport = viewport; |
| 422 | mFrame = frame; |
Mathias Agopian | 1b03149 | 2012-06-20 17:51:20 -0700 | [diff] [blame] | 423 | } |
Mathias Agopian | 1d12d8a | 2012-09-18 01:38:00 -0700 | [diff] [blame] | 424 | |
| 425 | void DisplayDevice::dump(String8& result, char* buffer, size_t SIZE) const { |
| 426 | const Transform& tr(mGlobalTransform); |
| 427 | snprintf(buffer, SIZE, |
| 428 | "+ DisplayDevice: %s\n" |
| 429 | " type=%x, layerStack=%u, (%4dx%4d), ANativeWindow=%p, orient=%2d (type=%08x), " |
Jamie Gennis | dd3cb84 | 2012-10-19 18:19:11 -0700 | [diff] [blame] | 430 | "flips=%u, isSecure=%d, secureVis=%d, acquired=%d, numLayers=%u\n" |
Mathias Agopian | 766dc49 | 2012-10-30 18:08:06 -0700 | [diff] [blame] | 431 | " 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] | 432 | "transform:[[%0.3f,%0.3f,%0.3f][%0.3f,%0.3f,%0.3f][%0.3f,%0.3f,%0.3f]]\n", |
Mathias Agopian | d56eff2 | 2012-09-19 16:25:29 -0700 | [diff] [blame] | 433 | mDisplayName.string(), mType, |
Mathias Agopian | 1d12d8a | 2012-09-18 01:38:00 -0700 | [diff] [blame] | 434 | mLayerStack, mDisplayWidth, mDisplayHeight, mNativeWindow.get(), |
| 435 | mOrientation, tr.getType(), getPageFlipCount(), |
Jamie Gennis | dd3cb84 | 2012-10-19 18:19:11 -0700 | [diff] [blame] | 436 | mIsSecure, mSecureLayerVisible, mScreenAcquired, mVisibleLayersSortedByZ.size(), |
Mathias Agopian | 1d12d8a | 2012-09-18 01:38:00 -0700 | [diff] [blame] | 437 | mViewport.left, mViewport.top, mViewport.right, mViewport.bottom, |
| 438 | mFrame.left, mFrame.top, mFrame.right, mFrame.bottom, |
Mathias Agopian | 766dc49 | 2012-10-30 18:08:06 -0700 | [diff] [blame] | 439 | mScissor.left, mScissor.top, mScissor.right, mScissor.bottom, |
Mathias Agopian | 1d12d8a | 2012-09-18 01:38:00 -0700 | [diff] [blame] | 440 | tr[0][0], tr[1][0], tr[2][0], |
| 441 | tr[0][1], tr[1][1], tr[2][1], |
| 442 | tr[0][2], tr[1][2], tr[2][2]); |
| 443 | |
| 444 | result.append(buffer); |
| 445 | |
Jesse Hall | 99c7dbb | 2013-03-14 14:29:29 -0700 | [diff] [blame] | 446 | String8 surfaceDump; |
| 447 | mDisplaySurface->dump(surfaceDump); |
| 448 | result.append(surfaceDump); |
Mathias Agopian | 1d12d8a | 2012-09-18 01:38:00 -0700 | [diff] [blame] | 449 | } |