blob: a379111c7be0f23655025f9d1658d13d2da6610c [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
Jamie Gennis1a4d8832012-08-02 20:11:05 -070030#include <gui/SurfaceTextureClient.h>
31
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080032#include <GLES/gl.h>
Mathias Agopian076b1cc2009-04-10 14:24:30 -070033#include <EGL/egl.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080034#include <EGL/eglext.h>
35
Mathias Agopian076b1cc2009-04-10 14:24:30 -070036#include <hardware/gralloc.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080037
Mathias Agopian1b031492012-06-20 17:51:20 -070038#include "DisplayHardware/FramebufferSurface.h"
Mathias Agopian1b031492012-06-20 17:51:20 -070039#include "DisplayHardware/HWComposer.h"
40
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -070041#include "DisplayDevice.h"
Mathias Agopian1f7bec62010-06-25 18:02:21 -070042#include "GLExtensions.h"
Mathias Agopianc7d14e22011-08-01 16:32:21 -070043#include "SurfaceFlinger.h"
Mathias Agopian921e6ac2012-07-23 23:11:29 -070044#include "LayerBase.h"
Mathias Agopian1f7bec62010-06-25 18:02:21 -070045
Mathias Agopiana4912602012-07-12 14:25:33 -070046// ----------------------------------------------------------------------------
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080047using namespace android;
Mathias Agopiana4912602012-07-12 14:25:33 -070048// ----------------------------------------------------------------------------
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080049
50static __attribute__((noinline))
51void checkGLErrors()
52{
Mathias Agopiancbb288b2009-09-07 16:32:45 -070053 do {
54 // there could be more than one error flag
55 GLenum error = glGetError();
56 if (error == GL_NO_ERROR)
57 break;
Steve Blocke6f43dd2012-01-06 19:20:56 +000058 ALOGE("GL error 0x%04x", int(error));
Mathias Agopiancbb288b2009-09-07 16:32:45 -070059 } while(true);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080060}
61
Mathias Agopiana4912602012-07-12 14:25:33 -070062// ----------------------------------------------------------------------------
63
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080064/*
65 * Initialize the display to the specified values.
66 *
67 */
68
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -070069DisplayDevice::DisplayDevice(
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080070 const sp<SurfaceFlinger>& flinger,
Mathias Agopiana4912602012-07-12 14:25:33 -070071 int display,
Jamie Gennis1a4d8832012-08-02 20:11:05 -070072 const sp<ANativeWindow>& nativeWindow,
73 const sp<FramebufferSurface>& framebufferSurface,
Mathias Agopiana4912602012-07-12 14:25:33 -070074 EGLConfig config)
Mathias Agopian92a979a2012-08-02 18:32:23 -070075 : mFlinger(flinger),
76 mId(display),
Jamie Gennis1a4d8832012-08-02 20:11:05 -070077 mNativeWindow(nativeWindow),
78 mFramebufferSurface(framebufferSurface),
Mathias Agopian92a979a2012-08-02 18:32:23 -070079 mDisplay(EGL_NO_DISPLAY),
80 mSurface(EGL_NO_SURFACE),
81 mContext(EGL_NO_CONTEXT),
Mathias Agopian92a979a2012-08-02 18:32:23 -070082 mDisplayWidth(), mDisplayHeight(), mFormat(),
83 mFlags(),
84 mPageFlipCount(),
Mathias Agopian92a979a2012-08-02 18:32:23 -070085 mSecureLayerVisible(false),
86 mScreenAcquired(false),
87 mOrientation(),
88 mLayerStack(0)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080089{
Mathias Agopiana4912602012-07-12 14:25:33 -070090 init(config);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080091}
92
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -070093DisplayDevice::~DisplayDevice() {
Mathias Agopian92a979a2012-08-02 18:32:23 -070094 if (mSurface != EGL_NO_SURFACE) {
95 eglDestroySurface(mDisplay, mSurface);
96 mSurface = EGL_NO_SURFACE;
97 }
98}
99
100bool DisplayDevice::isValid() const {
101 return mFlinger != NULL;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800102}
103
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700104int DisplayDevice::getWidth() const {
Mathias Agopiana4912602012-07-12 14:25:33 -0700105 return mDisplayWidth;
106}
107
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700108int DisplayDevice::getHeight() const {
Mathias Agopiana4912602012-07-12 14:25:33 -0700109 return mDisplayHeight;
110}
111
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700112PixelFormat DisplayDevice::getFormat() const {
Mathias Agopiana4912602012-07-12 14:25:33 -0700113 return mFormat;
114}
115
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700116EGLSurface DisplayDevice::getEGLSurface() const {
Mathias Agopiana4912602012-07-12 14:25:33 -0700117 return mSurface;
118}
119
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700120void DisplayDevice::init(EGLConfig config)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800121{
Mathias Agopiana4912602012-07-12 14:25:33 -0700122 ANativeWindow* const window = mNativeWindow.get();
123
Mathias Agopian61630912011-07-06 16:35:30 -0700124 int format;
Mathias Agopian61630912011-07-06 16:35:30 -0700125 window->query(window, NATIVE_WINDOW_FORMAT, &format);
Mathias Agopianb5dd9c02012-03-22 12:15:54 -0700126
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800127 /*
Mathias Agopiana4912602012-07-12 14:25:33 -0700128 * Create our display's surface
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800129 */
130
Mathias Agopiana4912602012-07-12 14:25:33 -0700131 EGLSurface surface;
132 EGLint w, h;
133 EGLDisplay display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
134 surface = eglCreateWindowSurface(display, config, window, NULL);
Mathias Agopian1b031492012-06-20 17:51:20 -0700135 eglQuerySurface(display, surface, EGL_WIDTH, &mDisplayWidth);
136 eglQuerySurface(display, surface, EGL_HEIGHT, &mDisplayHeight);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800137
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800138 mDisplay = display;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800139 mSurface = surface;
Mathias Agopiana4912602012-07-12 14:25:33 -0700140 mFormat = format;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700141 mPageFlipCount = 0;
Mathias Agopian1f7bec62010-06-25 18:02:21 -0700142
Mathias Agopian5f20e2d2012-08-10 18:50:38 -0700143 // external displays are always considered enabled
144 mScreenAcquired = mId >= DisplayDevice::DISPLAY_ID_COUNT;
145
Mathias Agopian98a121a2012-07-24 21:08:59 -0700146 // initialize the display orientation transform.
Mathias Agopian3165cc22012-08-08 19:42:09 -0700147 DisplayDevice::setOrientation(DisplayState::eOrientationDefault);
Mathias Agopiana350ff92010-08-10 17:14:02 -0700148}
149
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700150uint32_t DisplayDevice::getPageFlipCount() const {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700151 return mPageFlipCount;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800152}
153
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700154status_t DisplayDevice::compositionComplete() const {
Mathias Agopiana4912602012-07-12 14:25:33 -0700155 if (mFramebufferSurface == NULL) {
156 return NO_ERROR;
157 }
158 return mFramebufferSurface->compositionComplete();
Mathias Agopian74faca22009-09-17 16:18:16 -0700159}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800160
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700161void DisplayDevice::flip(const Region& dirty) const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800162{
163 checkGLErrors();
164
165 EGLDisplay dpy = mDisplay;
166 EGLSurface surface = mSurface;
167
Mathias Agopian5e78e092009-06-11 17:19:54 -0700168#ifdef EGL_ANDROID_swap_rectangle
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700169 if (mFlags & SWAP_RECTANGLE) {
Mathias Agopianb8a55602009-06-26 19:06:36 -0700170 const Region newDirty(dirty.intersect(bounds()));
171 const Rect b(newDirty.getBounds());
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700172 eglSetSwapRectangleANDROID(dpy, surface,
173 b.left, b.top, b.width(), b.height());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800174 }
Mathias Agopian5e78e092009-06-11 17:19:54 -0700175#endif
176
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700177 mPageFlipCount++;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800178}
179
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700180uint32_t DisplayDevice::getFlags() const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800181{
182 return mFlags;
183}
184
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700185void DisplayDevice::dump(String8& res) const
Erik Gilling1d21a9c2010-12-01 16:38:01 -0800186{
Mathias Agopiana4912602012-07-12 14:25:33 -0700187 if (mFramebufferSurface != NULL) {
188 mFramebufferSurface->dump(res);
189 }
Erik Gilling1d21a9c2010-12-01 16:38:01 -0800190}
Mathias Agopian1b031492012-06-20 17:51:20 -0700191
Mathias Agopian42977342012-08-05 00:40:46 -0700192void DisplayDevice::makeCurrent(const sp<const DisplayDevice>& hw, EGLContext ctx) {
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700193 EGLSurface sur = eglGetCurrentSurface(EGL_DRAW);
Mathias Agopian42977342012-08-05 00:40:46 -0700194 if (sur != hw->mSurface) {
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700195 EGLDisplay dpy = eglGetCurrentDisplay();
Mathias Agopian42977342012-08-05 00:40:46 -0700196 eglMakeCurrent(dpy, hw->mSurface, hw->mSurface, ctx);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700197 }
198}
199
Mathias Agopian1b031492012-06-20 17:51:20 -0700200// ----------------------------------------------------------------------------
201
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700202void DisplayDevice::setVisibleLayersSortedByZ(const Vector< sp<LayerBase> >& layers) {
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700203 mVisibleLayersSortedByZ = layers;
Mathias Agopianef7b9c72012-08-10 15:22:19 -0700204 mSecureLayerVisible = false;
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700205 size_t count = layers.size();
206 for (size_t i=0 ; i<count ; i++) {
207 if (layers[i]->isSecure()) {
208 mSecureLayerVisible = true;
209 }
210 }
211}
212
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700213Vector< sp<LayerBase> > DisplayDevice::getVisibleLayersSortedByZ() const {
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700214 return mVisibleLayersSortedByZ;
215}
216
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700217bool DisplayDevice::getSecureLayerVisible() const {
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700218 return mSecureLayerVisible;
219}
220
221// ----------------------------------------------------------------------------
222
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700223bool DisplayDevice::canDraw() const {
224 return mScreenAcquired;
225}
226
227void DisplayDevice::releaseScreen() const {
228 mScreenAcquired = false;
229}
230
231void DisplayDevice::acquireScreen() const {
232 mScreenAcquired = true;
233}
234
235bool DisplayDevice::isScreenAcquired() const {
236 return mScreenAcquired;
237}
238
239// ----------------------------------------------------------------------------
240
Mathias Agopian28947d72012-08-08 18:51:15 -0700241void DisplayDevice::setLayerStack(uint32_t stack) {
242 mLayerStack = stack;
243 dirtyRegion.set(bounds());
244}
245
246// ----------------------------------------------------------------------------
247
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700248status_t DisplayDevice::orientationToTransfrom(
Mathias Agopian1b031492012-06-20 17:51:20 -0700249 int orientation, int w, int h, Transform* tr)
250{
251 uint32_t flags = 0;
252 switch (orientation) {
Mathias Agopian3165cc22012-08-08 19:42:09 -0700253 case DisplayState::eOrientationDefault:
Mathias Agopian1b031492012-06-20 17:51:20 -0700254 flags = Transform::ROT_0;
255 break;
Mathias Agopian3165cc22012-08-08 19:42:09 -0700256 case DisplayState::eOrientation90:
Mathias Agopian1b031492012-06-20 17:51:20 -0700257 flags = Transform::ROT_90;
258 break;
Mathias Agopian3165cc22012-08-08 19:42:09 -0700259 case DisplayState::eOrientation180:
Mathias Agopian1b031492012-06-20 17:51:20 -0700260 flags = Transform::ROT_180;
261 break;
Mathias Agopian3165cc22012-08-08 19:42:09 -0700262 case DisplayState::eOrientation270:
Mathias Agopian1b031492012-06-20 17:51:20 -0700263 flags = Transform::ROT_270;
264 break;
265 default:
266 return BAD_VALUE;
267 }
268 tr->set(flags, w, h);
269 return NO_ERROR;
270}
271
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700272status_t DisplayDevice::setOrientation(int orientation) {
Mathias Agopian98a121a2012-07-24 21:08:59 -0700273 int w = mDisplayWidth;
274 int h = mDisplayHeight;
Mathias Agopian1b031492012-06-20 17:51:20 -0700275
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700276 DisplayDevice::orientationToTransfrom(
Mathias Agopian98a121a2012-07-24 21:08:59 -0700277 orientation, w, h, &mGlobalTransform);
Mathias Agopian3165cc22012-08-08 19:42:09 -0700278 if (orientation & DisplayState::eOrientationSwapMask) {
Mathias Agopian98a121a2012-07-24 21:08:59 -0700279 int tmp = w;
280 w = h;
281 h = tmp;
Mathias Agopian1b031492012-06-20 17:51:20 -0700282 }
Mathias Agopian1b031492012-06-20 17:51:20 -0700283 mOrientation = orientation;
Mathias Agopian92a979a2012-08-02 18:32:23 -0700284 dirtyRegion.set(bounds());
Mathias Agopian1b031492012-06-20 17:51:20 -0700285 return NO_ERROR;
286}