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 | |
Jamie Gennis | 1a4d883 | 2012-08-02 20:11:05 -0700 | [diff] [blame] | 30 | #include <gui/SurfaceTextureClient.h> |
| 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 | |
Mathias Agopian | 1b03149 | 2012-06-20 17:51:20 -0700 | [diff] [blame] | 38 | #include "DisplayHardware/FramebufferSurface.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 | 921e6ac | 2012-07-23 23:11:29 -0700 | [diff] [blame] | 45 | #include "LayerBase.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, |
Jamie Gennis | 1a4d883 | 2012-08-02 20:11:05 -0700 | [diff] [blame] | 75 | const sp<ANativeWindow>& nativeWindow, |
| 76 | const sp<FramebufferSurface>& framebufferSurface, |
Mathias Agopian | a491260 | 2012-07-12 14:25:33 -0700 | [diff] [blame] | 77 | EGLConfig config) |
Mathias Agopian | 92a979a | 2012-08-02 18:32:23 -0700 | [diff] [blame] | 78 | : mFlinger(flinger), |
Mathias Agopian | 3ee454a | 2012-08-27 16:28:24 -0700 | [diff] [blame] | 79 | mType(type), mHwcDisplayId(-1), |
Jamie Gennis | 1a4d883 | 2012-08-02 20:11:05 -0700 | [diff] [blame] | 80 | mNativeWindow(nativeWindow), |
| 81 | mFramebufferSurface(framebufferSurface), |
Mathias Agopian | 92a979a | 2012-08-02 18:32:23 -0700 | [diff] [blame] | 82 | mDisplay(EGL_NO_DISPLAY), |
| 83 | mSurface(EGL_NO_SURFACE), |
| 84 | mContext(EGL_NO_CONTEXT), |
Mathias Agopian | 92a979a | 2012-08-02 18:32:23 -0700 | [diff] [blame] | 85 | mDisplayWidth(), mDisplayHeight(), mFormat(), |
| 86 | mFlags(), |
| 87 | mPageFlipCount(), |
Jamie Gennis | dd3cb84 | 2012-10-19 18:19:11 -0700 | [diff] [blame^] | 88 | mIsSecure(isSecure), |
Mathias Agopian | 92a979a | 2012-08-02 18:32:23 -0700 | [diff] [blame] | 89 | mSecureLayerVisible(false), |
| 90 | mScreenAcquired(false), |
Mathias Agopian | da8d0a5 | 2012-09-04 15:05:38 -0700 | [diff] [blame] | 91 | mLayerStack(0), |
| 92 | mOrientation() |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 93 | { |
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 { |
Mathias Agopian | a491260 | 2012-07-12 14:25:33 -0700 | [diff] [blame] | 185 | if (mFramebufferSurface == NULL) { |
| 186 | return NO_ERROR; |
| 187 | } |
| 188 | return mFramebufferSurface->compositionComplete(); |
Mathias Agopian | 74faca2 | 2009-09-17 16:18:16 -0700 | [diff] [blame] | 189 | } |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 190 | |
Mathias Agopian | 0f2f5ff | 2012-07-31 23:09:07 -0700 | [diff] [blame] | 191 | void DisplayDevice::flip(const Region& dirty) const |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 192 | { |
| 193 | checkGLErrors(); |
| 194 | |
| 195 | EGLDisplay dpy = mDisplay; |
| 196 | EGLSurface surface = mSurface; |
| 197 | |
Mathias Agopian | 5e78e09 | 2009-06-11 17:19:54 -0700 | [diff] [blame] | 198 | #ifdef EGL_ANDROID_swap_rectangle |
Mathias Agopian | df3ca30 | 2009-05-04 19:29:25 -0700 | [diff] [blame] | 199 | if (mFlags & SWAP_RECTANGLE) { |
Mathias Agopian | b8a5560 | 2009-06-26 19:06:36 -0700 | [diff] [blame] | 200 | const Region newDirty(dirty.intersect(bounds())); |
| 201 | const Rect b(newDirty.getBounds()); |
Mathias Agopian | df3ca30 | 2009-05-04 19:29:25 -0700 | [diff] [blame] | 202 | eglSetSwapRectangleANDROID(dpy, surface, |
| 203 | b.left, b.top, b.width(), b.height()); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 204 | } |
Mathias Agopian | 5e78e09 | 2009-06-11 17:19:54 -0700 | [diff] [blame] | 205 | #endif |
Mathias Agopian | d870703 | 2012-09-18 01:21:55 -0700 | [diff] [blame] | 206 | |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 207 | mPageFlipCount++; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 208 | } |
| 209 | |
Mathias Agopian | da27af9 | 2012-09-13 18:17:13 -0700 | [diff] [blame] | 210 | void DisplayDevice::swapBuffers(HWComposer& hwc) const { |
Mathias Agopian | 52e2148 | 2012-09-24 18:07:21 -0700 | [diff] [blame] | 211 | EGLBoolean success = EGL_TRUE; |
Mathias Agopian | da27af9 | 2012-09-13 18:17:13 -0700 | [diff] [blame] | 212 | if (hwc.initCheck() != NO_ERROR) { |
| 213 | // no HWC, we call eglSwapBuffers() |
Mathias Agopian | 52e2148 | 2012-09-24 18:07:21 -0700 | [diff] [blame] | 214 | success = eglSwapBuffers(mDisplay, mSurface); |
Mathias Agopian | da27af9 | 2012-09-13 18:17:13 -0700 | [diff] [blame] | 215 | } else { |
Mathias Agopian | d870703 | 2012-09-18 01:21:55 -0700 | [diff] [blame] | 216 | // We have a valid HWC, but not all displays can use it, in particular |
| 217 | // the virtual displays are on their own. |
| 218 | // TODO: HWC 1.2 will allow virtual displays |
| 219 | if (mType >= DisplayDevice::DISPLAY_VIRTUAL) { |
| 220 | // always call eglSwapBuffers() for virtual displays |
Mathias Agopian | 52e2148 | 2012-09-24 18:07:21 -0700 | [diff] [blame] | 221 | success = eglSwapBuffers(mDisplay, mSurface); |
Mathias Agopian | d870703 | 2012-09-18 01:21:55 -0700 | [diff] [blame] | 222 | } else if (hwc.supportsFramebufferTarget()) { |
| 223 | // as of hwc 1.1 we always call eglSwapBuffers if we have some |
| 224 | // GLES layers |
| 225 | if (hwc.hasGlesComposition(mType)) { |
Mathias Agopian | 52e2148 | 2012-09-24 18:07:21 -0700 | [diff] [blame] | 226 | success = eglSwapBuffers(mDisplay, mSurface); |
Mathias Agopian | da27af9 | 2012-09-13 18:17:13 -0700 | [diff] [blame] | 227 | } |
Mathias Agopian | d870703 | 2012-09-18 01:21:55 -0700 | [diff] [blame] | 228 | } else { |
| 229 | // HWC doesn't have the framebuffer target, we don't call |
| 230 | // eglSwapBuffers(), since this is handled by HWComposer::commit(). |
Mathias Agopian | da27af9 | 2012-09-13 18:17:13 -0700 | [diff] [blame] | 231 | } |
| 232 | } |
Mathias Agopian | 52e2148 | 2012-09-24 18:07:21 -0700 | [diff] [blame] | 233 | |
Mathias Agopian | 3234138 | 2012-09-25 19:16:28 -0700 | [diff] [blame] | 234 | if (!success) { |
| 235 | EGLint error = eglGetError(); |
| 236 | if (error == EGL_CONTEXT_LOST || |
| 237 | mType == DisplayDevice::DISPLAY_PRIMARY) { |
| 238 | LOG_ALWAYS_FATAL("eglSwapBuffers(%p, %p) failed with 0x%08x", |
Mathias Agopian | b8fc00b | 2012-10-09 16:43:50 -0700 | [diff] [blame] | 239 | mDisplay, mSurface, error); |
Mathias Agopian | 3234138 | 2012-09-25 19:16:28 -0700 | [diff] [blame] | 240 | } |
| 241 | } |
Mathias Agopian | da27af9 | 2012-09-13 18:17:13 -0700 | [diff] [blame] | 242 | } |
| 243 | |
| 244 | void DisplayDevice::onSwapBuffersCompleted(HWComposer& hwc) const { |
| 245 | if (hwc.initCheck() == NO_ERROR) { |
| 246 | if (hwc.supportsFramebufferTarget()) { |
| 247 | int fd = hwc.getAndResetReleaseFenceFd(mType); |
| 248 | mFramebufferSurface->setReleaseFenceFd(fd); |
| 249 | } |
| 250 | } |
| 251 | } |
| 252 | |
Mathias Agopian | 0f2f5ff | 2012-07-31 23:09:07 -0700 | [diff] [blame] | 253 | uint32_t DisplayDevice::getFlags() const |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 254 | { |
| 255 | return mFlags; |
| 256 | } |
| 257 | |
Mathias Agopian | da8d0a5 | 2012-09-04 15:05:38 -0700 | [diff] [blame] | 258 | EGLBoolean DisplayDevice::makeCurrent(EGLDisplay dpy, |
| 259 | const sp<const DisplayDevice>& hw, EGLContext ctx) { |
| 260 | EGLBoolean result = EGL_TRUE; |
Mathias Agopian | 52bbb1a | 2012-07-31 19:01:53 -0700 | [diff] [blame] | 261 | EGLSurface sur = eglGetCurrentSurface(EGL_DRAW); |
Mathias Agopian | 4297734 | 2012-08-05 00:40:46 -0700 | [diff] [blame] | 262 | if (sur != hw->mSurface) { |
Mathias Agopian | da8d0a5 | 2012-09-04 15:05:38 -0700 | [diff] [blame] | 263 | result = eglMakeCurrent(dpy, hw->mSurface, hw->mSurface, ctx); |
| 264 | if (result == EGL_TRUE) { |
Mathias Agopian | bae92d0 | 2012-09-28 01:00:47 -0700 | [diff] [blame] | 265 | setViewportAndProjection(hw); |
Mathias Agopian | da8d0a5 | 2012-09-04 15:05:38 -0700 | [diff] [blame] | 266 | } |
Mathias Agopian | 52bbb1a | 2012-07-31 19:01:53 -0700 | [diff] [blame] | 267 | } |
Mathias Agopian | da8d0a5 | 2012-09-04 15:05:38 -0700 | [diff] [blame] | 268 | return result; |
Mathias Agopian | 52bbb1a | 2012-07-31 19:01:53 -0700 | [diff] [blame] | 269 | } |
| 270 | |
Mathias Agopian | bae92d0 | 2012-09-28 01:00:47 -0700 | [diff] [blame] | 271 | void DisplayDevice::setViewportAndProjection(const sp<const DisplayDevice>& hw) { |
| 272 | GLsizei w = hw->mDisplayWidth; |
| 273 | GLsizei h = hw->mDisplayHeight; |
| 274 | glViewport(0, 0, w, h); |
| 275 | glMatrixMode(GL_PROJECTION); |
| 276 | glLoadIdentity(); |
| 277 | // put the origin in the left-bottom corner |
| 278 | 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] | 279 | glMatrixMode(GL_MODELVIEW); |
Mathias Agopian | bae92d0 | 2012-09-28 01:00:47 -0700 | [diff] [blame] | 280 | } |
| 281 | |
Mathias Agopian | 1b03149 | 2012-06-20 17:51:20 -0700 | [diff] [blame] | 282 | // ---------------------------------------------------------------------------- |
| 283 | |
Mathias Agopian | 0f2f5ff | 2012-07-31 23:09:07 -0700 | [diff] [blame] | 284 | void DisplayDevice::setVisibleLayersSortedByZ(const Vector< sp<LayerBase> >& layers) { |
Mathias Agopian | 3b1d2b6 | 2012-07-11 13:48:17 -0700 | [diff] [blame] | 285 | mVisibleLayersSortedByZ = layers; |
Mathias Agopian | ef7b9c7 | 2012-08-10 15:22:19 -0700 | [diff] [blame] | 286 | mSecureLayerVisible = false; |
Mathias Agopian | 3b1d2b6 | 2012-07-11 13:48:17 -0700 | [diff] [blame] | 287 | size_t count = layers.size(); |
| 288 | for (size_t i=0 ; i<count ; i++) { |
| 289 | if (layers[i]->isSecure()) { |
| 290 | mSecureLayerVisible = true; |
| 291 | } |
| 292 | } |
| 293 | } |
| 294 | |
Mathias Agopian | 3ee454a | 2012-08-27 16:28:24 -0700 | [diff] [blame] | 295 | const Vector< sp<LayerBase> >& DisplayDevice::getVisibleLayersSortedByZ() const { |
Mathias Agopian | 3b1d2b6 | 2012-07-11 13:48:17 -0700 | [diff] [blame] | 296 | return mVisibleLayersSortedByZ; |
| 297 | } |
| 298 | |
Mathias Agopian | 0f2f5ff | 2012-07-31 23:09:07 -0700 | [diff] [blame] | 299 | bool DisplayDevice::getSecureLayerVisible() const { |
Mathias Agopian | 3b1d2b6 | 2012-07-11 13:48:17 -0700 | [diff] [blame] | 300 | return mSecureLayerVisible; |
| 301 | } |
| 302 | |
Mathias Agopian | cd60f99 | 2012-08-16 16:28:27 -0700 | [diff] [blame] | 303 | Region DisplayDevice::getDirtyRegion(bool repaintEverything) const { |
| 304 | Region dirty; |
Mathias Agopian | cd60f99 | 2012-08-16 16:28:27 -0700 | [diff] [blame] | 305 | if (repaintEverything) { |
| 306 | dirty.set(getBounds()); |
| 307 | } else { |
Mathias Agopian | da8d0a5 | 2012-09-04 15:05:38 -0700 | [diff] [blame] | 308 | const Transform& planeTransform(mGlobalTransform); |
Mathias Agopian | cd60f99 | 2012-08-16 16:28:27 -0700 | [diff] [blame] | 309 | dirty = planeTransform.transform(this->dirtyRegion); |
| 310 | dirty.andSelf(getBounds()); |
| 311 | } |
| 312 | return dirty; |
| 313 | } |
| 314 | |
Mathias Agopian | 3b1d2b6 | 2012-07-11 13:48:17 -0700 | [diff] [blame] | 315 | // ---------------------------------------------------------------------------- |
| 316 | |
Mathias Agopian | d3ee231 | 2012-08-02 14:01:42 -0700 | [diff] [blame] | 317 | bool DisplayDevice::canDraw() const { |
| 318 | return mScreenAcquired; |
| 319 | } |
| 320 | |
| 321 | void DisplayDevice::releaseScreen() const { |
| 322 | mScreenAcquired = false; |
| 323 | } |
| 324 | |
| 325 | void DisplayDevice::acquireScreen() const { |
| 326 | mScreenAcquired = true; |
| 327 | } |
| 328 | |
| 329 | bool DisplayDevice::isScreenAcquired() const { |
| 330 | return mScreenAcquired; |
| 331 | } |
| 332 | |
| 333 | // ---------------------------------------------------------------------------- |
| 334 | |
Mathias Agopian | 28947d7 | 2012-08-08 18:51:15 -0700 | [diff] [blame] | 335 | void DisplayDevice::setLayerStack(uint32_t stack) { |
| 336 | mLayerStack = stack; |
| 337 | dirtyRegion.set(bounds()); |
| 338 | } |
| 339 | |
| 340 | // ---------------------------------------------------------------------------- |
| 341 | |
Mathias Agopian | 0f2f5ff | 2012-07-31 23:09:07 -0700 | [diff] [blame] | 342 | status_t DisplayDevice::orientationToTransfrom( |
Mathias Agopian | 1b03149 | 2012-06-20 17:51:20 -0700 | [diff] [blame] | 343 | int orientation, int w, int h, Transform* tr) |
| 344 | { |
| 345 | uint32_t flags = 0; |
| 346 | switch (orientation) { |
Mathias Agopian | 3165cc2 | 2012-08-08 19:42:09 -0700 | [diff] [blame] | 347 | case DisplayState::eOrientationDefault: |
Mathias Agopian | 1b03149 | 2012-06-20 17:51:20 -0700 | [diff] [blame] | 348 | flags = Transform::ROT_0; |
| 349 | break; |
Mathias Agopian | 3165cc2 | 2012-08-08 19:42:09 -0700 | [diff] [blame] | 350 | case DisplayState::eOrientation90: |
Mathias Agopian | 1b03149 | 2012-06-20 17:51:20 -0700 | [diff] [blame] | 351 | flags = Transform::ROT_90; |
| 352 | break; |
Mathias Agopian | 3165cc2 | 2012-08-08 19:42:09 -0700 | [diff] [blame] | 353 | case DisplayState::eOrientation180: |
Mathias Agopian | 1b03149 | 2012-06-20 17:51:20 -0700 | [diff] [blame] | 354 | flags = Transform::ROT_180; |
| 355 | break; |
Mathias Agopian | 3165cc2 | 2012-08-08 19:42:09 -0700 | [diff] [blame] | 356 | case DisplayState::eOrientation270: |
Mathias Agopian | 1b03149 | 2012-06-20 17:51:20 -0700 | [diff] [blame] | 357 | flags = Transform::ROT_270; |
| 358 | break; |
| 359 | default: |
| 360 | return BAD_VALUE; |
| 361 | } |
| 362 | tr->set(flags, w, h); |
| 363 | return NO_ERROR; |
| 364 | } |
| 365 | |
Mathias Agopian | 00e8c7a | 2012-09-04 19:30:46 -0700 | [diff] [blame] | 366 | void DisplayDevice::setProjection(int orientation, |
| 367 | const Rect& viewport, const Rect& frame) { |
Mathias Agopian | da8d0a5 | 2012-09-04 15:05:38 -0700 | [diff] [blame] | 368 | mOrientation = orientation; |
Mathias Agopian | 00e8c7a | 2012-09-04 19:30:46 -0700 | [diff] [blame] | 369 | mViewport = viewport; |
| 370 | mFrame = frame; |
Mathias Agopian | da8d0a5 | 2012-09-04 15:05:38 -0700 | [diff] [blame] | 371 | updateGeometryTransform(); |
| 372 | } |
| 373 | |
Mathias Agopian | da8d0a5 | 2012-09-04 15:05:38 -0700 | [diff] [blame] | 374 | void DisplayDevice::updateGeometryTransform() { |
Mathias Agopian | 98a121a | 2012-07-24 21:08:59 -0700 | [diff] [blame] | 375 | int w = mDisplayWidth; |
| 376 | int h = mDisplayHeight; |
Jeff Brown | 6e220a6 | 2012-09-13 19:22:41 -0700 | [diff] [blame] | 377 | Transform TL, TP, R, S; |
Mathias Agopian | da8d0a5 | 2012-09-04 15:05:38 -0700 | [diff] [blame] | 378 | if (DisplayDevice::orientationToTransfrom( |
| 379 | mOrientation, w, h, &R) == NO_ERROR) { |
| 380 | dirtyRegion.set(bounds()); |
Mathias Agopian | 1b03149 | 2012-06-20 17:51:20 -0700 | [diff] [blame] | 381 | |
Mathias Agopian | da8d0a5 | 2012-09-04 15:05:38 -0700 | [diff] [blame] | 382 | Rect viewport(mViewport); |
| 383 | Rect frame(mFrame); |
| 384 | |
| 385 | if (!frame.isValid()) { |
| 386 | // the destination frame can be invalid if it has never been set, |
| 387 | // in that case we assume the whole display frame. |
| 388 | frame = Rect(w, h); |
| 389 | } |
| 390 | |
| 391 | if (viewport.isEmpty()) { |
| 392 | // viewport can be invalid if it has never been set, in that case |
| 393 | // we assume the whole display size. |
| 394 | // it's also invalid to have an empty viewport, so we handle that |
| 395 | // case in the same way. |
| 396 | viewport = Rect(w, h); |
| 397 | if (R.getOrientation() & Transform::ROT_90) { |
| 398 | // viewport is always specified in the logical orientation |
| 399 | // of the display (ie: post-rotation). |
| 400 | swap(viewport.right, viewport.bottom); |
| 401 | } |
| 402 | } |
| 403 | |
| 404 | float src_width = viewport.width(); |
| 405 | float src_height = viewport.height(); |
| 406 | float dst_width = frame.width(); |
| 407 | float dst_height = frame.height(); |
Jesse Hall | 6360ec4 | 2012-09-12 13:49:10 -0700 | [diff] [blame] | 408 | if (src_width != dst_width || src_height != dst_height) { |
Mathias Agopian | da8d0a5 | 2012-09-04 15:05:38 -0700 | [diff] [blame] | 409 | float sx = dst_width / src_width; |
| 410 | float sy = dst_height / src_height; |
| 411 | S.set(sx, 0, 0, sy); |
| 412 | } |
Jesse Hall | 6360ec4 | 2012-09-12 13:49:10 -0700 | [diff] [blame] | 413 | |
Mathias Agopian | da8d0a5 | 2012-09-04 15:05:38 -0700 | [diff] [blame] | 414 | float src_x = viewport.left; |
| 415 | float src_y = viewport.top; |
| 416 | float dst_x = frame.left; |
| 417 | float dst_y = frame.top; |
Jeff Brown | 6e220a6 | 2012-09-13 19:22:41 -0700 | [diff] [blame] | 418 | TL.set(-src_x, -src_y); |
| 419 | TP.set(dst_x, dst_y); |
Mathias Agopian | da8d0a5 | 2012-09-04 15:05:38 -0700 | [diff] [blame] | 420 | |
Jeff Brown | 6e220a6 | 2012-09-13 19:22:41 -0700 | [diff] [blame] | 421 | // The viewport and frame are both in the logical orientation. |
| 422 | // Apply the logical translation, scale to physical size, apply the |
| 423 | // physical translation and finally rotate to the physical orientation. |
| 424 | mGlobalTransform = R * TP * S * TL; |
Mathias Agopian | eba8c68 | 2012-09-19 23:14:45 -0700 | [diff] [blame] | 425 | |
| 426 | const uint8_t type = mGlobalTransform.getType(); |
| 427 | mNeedsFiltering = (!mGlobalTransform.preserveRects() || |
| 428 | (type >= Transform::SCALE)); |
Mathias Agopian | 1b03149 | 2012-06-20 17:51:20 -0700 | [diff] [blame] | 429 | } |
Mathias Agopian | 1b03149 | 2012-06-20 17:51:20 -0700 | [diff] [blame] | 430 | } |
Mathias Agopian | 1d12d8a | 2012-09-18 01:38:00 -0700 | [diff] [blame] | 431 | |
| 432 | void DisplayDevice::dump(String8& result, char* buffer, size_t SIZE) const { |
| 433 | const Transform& tr(mGlobalTransform); |
| 434 | snprintf(buffer, SIZE, |
| 435 | "+ DisplayDevice: %s\n" |
| 436 | " type=%x, layerStack=%u, (%4dx%4d), ANativeWindow=%p, orient=%2d (type=%08x), " |
Jamie Gennis | dd3cb84 | 2012-10-19 18:19:11 -0700 | [diff] [blame^] | 437 | "flips=%u, isSecure=%d, secureVis=%d, acquired=%d, numLayers=%u\n" |
Mathias Agopian | 1d12d8a | 2012-09-18 01:38:00 -0700 | [diff] [blame] | 438 | " v:[%d,%d,%d,%d], f:[%d,%d,%d,%d], " |
| 439 | "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] | 440 | mDisplayName.string(), mType, |
Mathias Agopian | 1d12d8a | 2012-09-18 01:38:00 -0700 | [diff] [blame] | 441 | mLayerStack, mDisplayWidth, mDisplayHeight, mNativeWindow.get(), |
| 442 | mOrientation, tr.getType(), getPageFlipCount(), |
Jamie Gennis | dd3cb84 | 2012-10-19 18:19:11 -0700 | [diff] [blame^] | 443 | mIsSecure, mSecureLayerVisible, mScreenAcquired, mVisibleLayersSortedByZ.size(), |
Mathias Agopian | 1d12d8a | 2012-09-18 01:38:00 -0700 | [diff] [blame] | 444 | mViewport.left, mViewport.top, mViewport.right, mViewport.bottom, |
| 445 | mFrame.left, mFrame.top, mFrame.right, mFrame.bottom, |
| 446 | tr[0][0], tr[1][0], tr[2][0], |
| 447 | tr[0][1], tr[1][1], tr[2][1], |
| 448 | tr[0][2], tr[1][2], tr[2][2]); |
| 449 | |
| 450 | result.append(buffer); |
| 451 | |
| 452 | String8 fbtargetDump; |
| 453 | if (mFramebufferSurface != NULL) { |
| 454 | mFramebufferSurface->dump(fbtargetDump); |
| 455 | result.append(fbtargetDump); |
| 456 | } |
| 457 | } |