blob: e3655f8e0b35662285edbffea973e921bb1f4eb2 [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),
82 mDpiX(), mDpiY(),
Mathias Agopian92a979a2012-08-02 18:32:23 -070083 mDensity(),
84 mDisplayWidth(), mDisplayHeight(), mFormat(),
85 mFlags(),
86 mPageFlipCount(),
Mathias Agopian92a979a2012-08-02 18:32:23 -070087 mSecureLayerVisible(false),
88 mScreenAcquired(false),
89 mOrientation(),
90 mLayerStack(0)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080091{
Mathias Agopiana4912602012-07-12 14:25:33 -070092 init(config);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080093}
94
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -070095DisplayDevice::~DisplayDevice() {
Mathias Agopian92a979a2012-08-02 18:32:23 -070096 if (mSurface != EGL_NO_SURFACE) {
97 eglDestroySurface(mDisplay, mSurface);
98 mSurface = EGL_NO_SURFACE;
99 }
100}
101
102bool DisplayDevice::isValid() const {
103 return mFlinger != NULL;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800104}
105
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700106float DisplayDevice::getDpiX() const {
Mathias Agopiana4912602012-07-12 14:25:33 -0700107 return mDpiX;
Mathias Agopian3d64e732011-04-18 15:59:24 -0700108}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800109
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700110float DisplayDevice::getDpiY() const {
Mathias Agopiana4912602012-07-12 14:25:33 -0700111 return mDpiY;
Mathias Agopian61630912011-07-06 16:35:30 -0700112}
113
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700114float DisplayDevice::getDensity() const {
Mathias Agopiana4912602012-07-12 14:25:33 -0700115 return mDensity;
116}
Mathias Agopian61630912011-07-06 16:35:30 -0700117
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700118int DisplayDevice::getWidth() const {
Mathias Agopiana4912602012-07-12 14:25:33 -0700119 return mDisplayWidth;
120}
121
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700122int DisplayDevice::getHeight() const {
Mathias Agopiana4912602012-07-12 14:25:33 -0700123 return mDisplayHeight;
124}
125
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700126PixelFormat DisplayDevice::getFormat() const {
Mathias Agopiana4912602012-07-12 14:25:33 -0700127 return mFormat;
128}
129
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700130EGLSurface DisplayDevice::getEGLSurface() const {
Mathias Agopiana4912602012-07-12 14:25:33 -0700131 return mSurface;
132}
133
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700134void DisplayDevice::init(EGLConfig config)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800135{
Mathias Agopiana4912602012-07-12 14:25:33 -0700136 ANativeWindow* const window = mNativeWindow.get();
137
Mathias Agopian61630912011-07-06 16:35:30 -0700138 int format;
Mathias Agopian61630912011-07-06 16:35:30 -0700139 window->query(window, NATIVE_WINDOW_FORMAT, &format);
Mathias Agopiana4912602012-07-12 14:25:33 -0700140 mDpiX = window->xdpi;
141 mDpiY = window->ydpi;
Mathias Agopiana4912602012-07-12 14:25:33 -0700142
143 // TODO: Not sure if display density should handled by SF any longer
Mathias Agopianb5dd9c02012-03-22 12:15:54 -0700144 class Density {
145 static int getDensityFromProperty(char const* propName) {
146 char property[PROPERTY_VALUE_MAX];
147 int density = 0;
148 if (property_get(propName, property, NULL) > 0) {
149 density = atoi(property);
150 }
151 return density;
152 }
153 public:
154 static int getEmuDensity() {
155 return getDensityFromProperty("qemu.sf.lcd_density"); }
156 static int getBuildDensity() {
157 return getDensityFromProperty("ro.sf.lcd_density"); }
158 };
Mathias Agopianb5dd9c02012-03-22 12:15:54 -0700159 // The density of the device is provided by a build property
160 mDensity = Density::getBuildDensity() / 160.0f;
Mathias Agopianb5dd9c02012-03-22 12:15:54 -0700161 if (mDensity == 0) {
162 // the build doesn't provide a density -- this is wrong!
163 // use xdpi instead
164 ALOGE("ro.sf.lcd_density must be defined as a build property");
165 mDensity = mDpiX / 160.0f;
166 }
Mathias Agopianb5dd9c02012-03-22 12:15:54 -0700167 if (Density::getEmuDensity()) {
168 // if "qemu.sf.lcd_density" is specified, it overrides everything
169 mDpiX = mDpiY = mDensity = Density::getEmuDensity();
170 mDensity /= 160.0f;
171 }
172
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800173 /*
Mathias Agopiana4912602012-07-12 14:25:33 -0700174 * Create our display's surface
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800175 */
176
Mathias Agopiana4912602012-07-12 14:25:33 -0700177 EGLSurface surface;
178 EGLint w, h;
179 EGLDisplay display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
180 surface = eglCreateWindowSurface(display, config, window, NULL);
Mathias Agopian1b031492012-06-20 17:51:20 -0700181 eglQuerySurface(display, surface, EGL_WIDTH, &mDisplayWidth);
182 eglQuerySurface(display, surface, EGL_HEIGHT, &mDisplayHeight);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800183
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800184 mDisplay = display;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800185 mSurface = surface;
Mathias Agopiana4912602012-07-12 14:25:33 -0700186 mFormat = format;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700187 mPageFlipCount = 0;
Mathias Agopian1f7bec62010-06-25 18:02:21 -0700188
Mathias Agopian98a121a2012-07-24 21:08:59 -0700189 // initialize the display orientation transform.
Mathias Agopian3165cc22012-08-08 19:42:09 -0700190 DisplayDevice::setOrientation(DisplayState::eOrientationDefault);
Mathias Agopiana350ff92010-08-10 17:14:02 -0700191}
192
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700193uint32_t DisplayDevice::getPageFlipCount() const {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700194 return mPageFlipCount;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800195}
196
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700197status_t DisplayDevice::compositionComplete() const {
Mathias Agopiana4912602012-07-12 14:25:33 -0700198 if (mFramebufferSurface == NULL) {
199 return NO_ERROR;
200 }
201 return mFramebufferSurface->compositionComplete();
Mathias Agopian74faca22009-09-17 16:18:16 -0700202}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800203
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700204void DisplayDevice::flip(const Region& dirty) const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800205{
206 checkGLErrors();
207
208 EGLDisplay dpy = mDisplay;
209 EGLSurface surface = mSurface;
210
Mathias Agopian5e78e092009-06-11 17:19:54 -0700211#ifdef EGL_ANDROID_swap_rectangle
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700212 if (mFlags & SWAP_RECTANGLE) {
Mathias Agopianb8a55602009-06-26 19:06:36 -0700213 const Region newDirty(dirty.intersect(bounds()));
214 const Rect b(newDirty.getBounds());
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700215 eglSetSwapRectangleANDROID(dpy, surface,
216 b.left, b.top, b.width(), b.height());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800217 }
Mathias Agopian5e78e092009-06-11 17:19:54 -0700218#endif
219
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700220 mPageFlipCount++;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800221}
222
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700223uint32_t DisplayDevice::getFlags() const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800224{
225 return mFlags;
226}
227
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700228void DisplayDevice::dump(String8& res) const
Erik Gilling1d21a9c2010-12-01 16:38:01 -0800229{
Mathias Agopiana4912602012-07-12 14:25:33 -0700230 if (mFramebufferSurface != NULL) {
231 mFramebufferSurface->dump(res);
232 }
Erik Gilling1d21a9c2010-12-01 16:38:01 -0800233}
Mathias Agopian1b031492012-06-20 17:51:20 -0700234
Mathias Agopian42977342012-08-05 00:40:46 -0700235void DisplayDevice::makeCurrent(const sp<const DisplayDevice>& hw, EGLContext ctx) {
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700236 EGLSurface sur = eglGetCurrentSurface(EGL_DRAW);
Mathias Agopian42977342012-08-05 00:40:46 -0700237 if (sur != hw->mSurface) {
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700238 EGLDisplay dpy = eglGetCurrentDisplay();
Mathias Agopian42977342012-08-05 00:40:46 -0700239 eglMakeCurrent(dpy, hw->mSurface, hw->mSurface, ctx);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700240 }
241}
242
Mathias Agopian1b031492012-06-20 17:51:20 -0700243// ----------------------------------------------------------------------------
244
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700245void DisplayDevice::setVisibleLayersSortedByZ(const Vector< sp<LayerBase> >& layers) {
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700246 mVisibleLayersSortedByZ = layers;
247 size_t count = layers.size();
248 for (size_t i=0 ; i<count ; i++) {
249 if (layers[i]->isSecure()) {
250 mSecureLayerVisible = true;
251 }
252 }
253}
254
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700255Vector< sp<LayerBase> > DisplayDevice::getVisibleLayersSortedByZ() const {
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700256 return mVisibleLayersSortedByZ;
257}
258
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700259bool DisplayDevice::getSecureLayerVisible() const {
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700260 return mSecureLayerVisible;
261}
262
263// ----------------------------------------------------------------------------
264
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700265bool DisplayDevice::canDraw() const {
266 return mScreenAcquired;
267}
268
269void DisplayDevice::releaseScreen() const {
270 mScreenAcquired = false;
271}
272
273void DisplayDevice::acquireScreen() const {
274 mScreenAcquired = true;
275}
276
277bool DisplayDevice::isScreenAcquired() const {
278 return mScreenAcquired;
279}
280
281// ----------------------------------------------------------------------------
282
Mathias Agopian28947d72012-08-08 18:51:15 -0700283void DisplayDevice::setLayerStack(uint32_t stack) {
284 mLayerStack = stack;
285 dirtyRegion.set(bounds());
286}
287
288// ----------------------------------------------------------------------------
289
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700290status_t DisplayDevice::orientationToTransfrom(
Mathias Agopian1b031492012-06-20 17:51:20 -0700291 int orientation, int w, int h, Transform* tr)
292{
293 uint32_t flags = 0;
294 switch (orientation) {
Mathias Agopian3165cc22012-08-08 19:42:09 -0700295 case DisplayState::eOrientationDefault:
Mathias Agopian1b031492012-06-20 17:51:20 -0700296 flags = Transform::ROT_0;
297 break;
Mathias Agopian3165cc22012-08-08 19:42:09 -0700298 case DisplayState::eOrientation90:
Mathias Agopian1b031492012-06-20 17:51:20 -0700299 flags = Transform::ROT_90;
300 break;
Mathias Agopian3165cc22012-08-08 19:42:09 -0700301 case DisplayState::eOrientation180:
Mathias Agopian1b031492012-06-20 17:51:20 -0700302 flags = Transform::ROT_180;
303 break;
Mathias Agopian3165cc22012-08-08 19:42:09 -0700304 case DisplayState::eOrientation270:
Mathias Agopian1b031492012-06-20 17:51:20 -0700305 flags = Transform::ROT_270;
306 break;
307 default:
308 return BAD_VALUE;
309 }
310 tr->set(flags, w, h);
311 return NO_ERROR;
312}
313
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700314status_t DisplayDevice::setOrientation(int orientation) {
Mathias Agopian98a121a2012-07-24 21:08:59 -0700315 int w = mDisplayWidth;
316 int h = mDisplayHeight;
Mathias Agopian1b031492012-06-20 17:51:20 -0700317
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700318 DisplayDevice::orientationToTransfrom(
Mathias Agopian98a121a2012-07-24 21:08:59 -0700319 orientation, w, h, &mGlobalTransform);
Mathias Agopian3165cc22012-08-08 19:42:09 -0700320 if (orientation & DisplayState::eOrientationSwapMask) {
Mathias Agopian98a121a2012-07-24 21:08:59 -0700321 int tmp = w;
322 w = h;
323 h = tmp;
Mathias Agopian1b031492012-06-20 17:51:20 -0700324 }
Mathias Agopian1b031492012-06-20 17:51:20 -0700325 mOrientation = orientation;
Mathias Agopian92a979a2012-08-02 18:32:23 -0700326 dirtyRegion.set(bounds());
Mathias Agopian1b031492012-06-20 17:51:20 -0700327 return NO_ERROR;
328}