blob: 822b2b56f1cfc9a5714605b3b2b83bf5868cb768 [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
30#include <GLES/gl.h>
Mathias Agopian076b1cc2009-04-10 14:24:30 -070031#include <EGL/egl.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080032#include <EGL/eglext.h>
33
Mathias Agopian076b1cc2009-04-10 14:24:30 -070034#include <hardware/gralloc.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080035
Mathias Agopian1b031492012-06-20 17:51:20 -070036#include "DisplayHardware/FramebufferSurface.h"
Mathias Agopian1b031492012-06-20 17:51:20 -070037#include "DisplayHardware/HWComposer.h"
38
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -070039#include "DisplayDevice.h"
Mathias Agopian1f7bec62010-06-25 18:02:21 -070040#include "GLExtensions.h"
Mathias Agopianc7d14e22011-08-01 16:32:21 -070041#include "SurfaceFlinger.h"
Mathias Agopian921e6ac2012-07-23 23:11:29 -070042#include "LayerBase.h"
Mathias Agopian1f7bec62010-06-25 18:02:21 -070043
Mathias Agopiana4912602012-07-12 14:25:33 -070044// ----------------------------------------------------------------------------
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080045using namespace android;
Mathias Agopiana4912602012-07-12 14:25:33 -070046// ----------------------------------------------------------------------------
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080047
48static __attribute__((noinline))
49void checkGLErrors()
50{
Mathias Agopiancbb288b2009-09-07 16:32:45 -070051 do {
52 // there could be more than one error flag
53 GLenum error = glGetError();
54 if (error == GL_NO_ERROR)
55 break;
Steve Blocke6f43dd2012-01-06 19:20:56 +000056 ALOGE("GL error 0x%04x", int(error));
Mathias Agopiancbb288b2009-09-07 16:32:45 -070057 } while(true);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080058}
59
60static __attribute__((noinline))
61void checkEGLErrors(const char* token)
62{
Mathias Agopian870b8aa2012-02-24 16:42:46 -080063 struct EGLUtils {
64 static const char *strerror(EGLint err) {
65 switch (err){
66 case EGL_SUCCESS: return "EGL_SUCCESS";
67 case EGL_NOT_INITIALIZED: return "EGL_NOT_INITIALIZED";
68 case EGL_BAD_ACCESS: return "EGL_BAD_ACCESS";
69 case EGL_BAD_ALLOC: return "EGL_BAD_ALLOC";
70 case EGL_BAD_ATTRIBUTE: return "EGL_BAD_ATTRIBUTE";
71 case EGL_BAD_CONFIG: return "EGL_BAD_CONFIG";
72 case EGL_BAD_CONTEXT: return "EGL_BAD_CONTEXT";
73 case EGL_BAD_CURRENT_SURFACE: return "EGL_BAD_CURRENT_SURFACE";
74 case EGL_BAD_DISPLAY: return "EGL_BAD_DISPLAY";
75 case EGL_BAD_MATCH: return "EGL_BAD_MATCH";
76 case EGL_BAD_NATIVE_PIXMAP: return "EGL_BAD_NATIVE_PIXMAP";
77 case EGL_BAD_NATIVE_WINDOW: return "EGL_BAD_NATIVE_WINDOW";
78 case EGL_BAD_PARAMETER: return "EGL_BAD_PARAMETER";
79 case EGL_BAD_SURFACE: return "EGL_BAD_SURFACE";
80 case EGL_CONTEXT_LOST: return "EGL_CONTEXT_LOST";
81 default: return "UNKNOWN";
82 }
83 }
84 };
85
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080086 EGLint error = eglGetError();
Mathias Agopiancbb288b2009-09-07 16:32:45 -070087 if (error && error != EGL_SUCCESS) {
Steve Blocke6f43dd2012-01-06 19:20:56 +000088 ALOGE("%s: EGL error 0x%04x (%s)",
Mathias Agopian0928e312009-08-07 16:38:10 -070089 token, int(error), EGLUtils::strerror(error));
Mathias Agopiancbb288b2009-09-07 16:32:45 -070090 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080091}
92
Mathias Agopiana4912602012-07-12 14:25:33 -070093// ----------------------------------------------------------------------------
94
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080095/*
96 * Initialize the display to the specified values.
97 *
98 */
99
Mathias Agopian92a979a2012-08-02 18:32:23 -0700100DisplayDevice::DisplayDevice()
101 : mId(0),
102 mDisplay(EGL_NO_DISPLAY),
103 mSurface(EGL_NO_SURFACE),
104 mContext(EGL_NO_CONTEXT)
105{
106}
107
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700108DisplayDevice::DisplayDevice(
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800109 const sp<SurfaceFlinger>& flinger,
Mathias Agopiana4912602012-07-12 14:25:33 -0700110 int display,
Mathias Agopiand8552d72012-08-04 21:39:11 -0700111 const sp<ANativeWindow>& surface,
Mathias Agopiana4912602012-07-12 14:25:33 -0700112 EGLConfig config)
Mathias Agopian92a979a2012-08-02 18:32:23 -0700113 : mFlinger(flinger),
114 mId(display),
115 mNativeWindow(surface),
116 mDisplay(EGL_NO_DISPLAY),
117 mSurface(EGL_NO_SURFACE),
118 mContext(EGL_NO_CONTEXT),
119 mDpiX(), mDpiY(),
Mathias Agopian92a979a2012-08-02 18:32:23 -0700120 mDensity(),
121 mDisplayWidth(), mDisplayHeight(), mFormat(),
122 mFlags(),
123 mPageFlipCount(),
Mathias Agopian92a979a2012-08-02 18:32:23 -0700124 mSecureLayerVisible(false),
125 mScreenAcquired(false),
126 mOrientation(),
127 mLayerStack(0)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800128{
Mathias Agopiana4912602012-07-12 14:25:33 -0700129 init(config);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800130}
131
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700132DisplayDevice::~DisplayDevice() {
Mathias Agopian92a979a2012-08-02 18:32:23 -0700133 // DO NOT call terminate() from here, because we create
134 // temporaries of this class (on the stack typically), and we don't
135 // want to destroy the EGLSurface in that case
136}
137
138void DisplayDevice::terminate() {
139 if (mSurface != EGL_NO_SURFACE) {
140 eglDestroySurface(mDisplay, mSurface);
141 mSurface = EGL_NO_SURFACE;
142 }
143}
144
145bool DisplayDevice::isValid() const {
146 return mFlinger != NULL;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800147}
148
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700149float DisplayDevice::getDpiX() const {
Mathias Agopiana4912602012-07-12 14:25:33 -0700150 return mDpiX;
Mathias Agopian3d64e732011-04-18 15:59:24 -0700151}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800152
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700153float DisplayDevice::getDpiY() const {
Mathias Agopiana4912602012-07-12 14:25:33 -0700154 return mDpiY;
Mathias Agopian61630912011-07-06 16:35:30 -0700155}
156
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700157float DisplayDevice::getDensity() const {
Mathias Agopiana4912602012-07-12 14:25:33 -0700158 return mDensity;
159}
Mathias Agopian61630912011-07-06 16:35:30 -0700160
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700161int DisplayDevice::getWidth() const {
Mathias Agopiana4912602012-07-12 14:25:33 -0700162 return mDisplayWidth;
163}
164
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700165int DisplayDevice::getHeight() const {
Mathias Agopiana4912602012-07-12 14:25:33 -0700166 return mDisplayHeight;
167}
168
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700169PixelFormat DisplayDevice::getFormat() const {
Mathias Agopiana4912602012-07-12 14:25:33 -0700170 return mFormat;
171}
172
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700173EGLSurface DisplayDevice::getEGLSurface() const {
Mathias Agopiana4912602012-07-12 14:25:33 -0700174 return mSurface;
175}
176
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700177void DisplayDevice::init(EGLConfig config)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800178{
Mathias Agopiana4912602012-07-12 14:25:33 -0700179 ANativeWindow* const window = mNativeWindow.get();
180
181 int concreteType;
182 window->query(window, NATIVE_WINDOW_CONCRETE_TYPE, &concreteType);
183 if (concreteType == NATIVE_WINDOW_FRAMEBUFFER) {
184 mFramebufferSurface = static_cast<FramebufferSurface *>(mNativeWindow.get());
Mathias Agopian1f339ff2011-07-01 17:08:43 -0700185 }
186
Mathias Agopian61630912011-07-06 16:35:30 -0700187 int format;
Mathias Agopian61630912011-07-06 16:35:30 -0700188 window->query(window, NATIVE_WINDOW_FORMAT, &format);
Mathias Agopiana4912602012-07-12 14:25:33 -0700189 mDpiX = window->xdpi;
190 mDpiY = window->ydpi;
Mathias Agopiana4912602012-07-12 14:25:33 -0700191
192 // TODO: Not sure if display density should handled by SF any longer
Mathias Agopianb5dd9c02012-03-22 12:15:54 -0700193 class Density {
194 static int getDensityFromProperty(char const* propName) {
195 char property[PROPERTY_VALUE_MAX];
196 int density = 0;
197 if (property_get(propName, property, NULL) > 0) {
198 density = atoi(property);
199 }
200 return density;
201 }
202 public:
203 static int getEmuDensity() {
204 return getDensityFromProperty("qemu.sf.lcd_density"); }
205 static int getBuildDensity() {
206 return getDensityFromProperty("ro.sf.lcd_density"); }
207 };
Mathias Agopianb5dd9c02012-03-22 12:15:54 -0700208 // The density of the device is provided by a build property
209 mDensity = Density::getBuildDensity() / 160.0f;
Mathias Agopianb5dd9c02012-03-22 12:15:54 -0700210 if (mDensity == 0) {
211 // the build doesn't provide a density -- this is wrong!
212 // use xdpi instead
213 ALOGE("ro.sf.lcd_density must be defined as a build property");
214 mDensity = mDpiX / 160.0f;
215 }
Mathias Agopianb5dd9c02012-03-22 12:15:54 -0700216 if (Density::getEmuDensity()) {
217 // if "qemu.sf.lcd_density" is specified, it overrides everything
218 mDpiX = mDpiY = mDensity = Density::getEmuDensity();
219 mDensity /= 160.0f;
220 }
221
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800222 /*
Mathias Agopiana4912602012-07-12 14:25:33 -0700223 * Create our display's surface
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800224 */
225
Mathias Agopiana4912602012-07-12 14:25:33 -0700226 EGLSurface surface;
227 EGLint w, h;
228 EGLDisplay display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
229 surface = eglCreateWindowSurface(display, config, window, NULL);
Mathias Agopian1b031492012-06-20 17:51:20 -0700230 eglQuerySurface(display, surface, EGL_WIDTH, &mDisplayWidth);
231 eglQuerySurface(display, surface, EGL_HEIGHT, &mDisplayHeight);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800232
Mathias Agopiana4912602012-07-12 14:25:33 -0700233 if (mFramebufferSurface != NULL) {
234 if (mFramebufferSurface->isUpdateOnDemand()) {
235 mFlags |= PARTIAL_UPDATES;
236 // if we have partial updates, we definitely don't need to
237 // preserve the backbuffer, which may be costly.
238 eglSurfaceAttrib(display, surface,
239 EGL_SWAP_BEHAVIOR, EGL_BUFFER_DESTROYED);
240 }
Mathias Agopian0928bee2009-09-16 20:15:42 -0700241 }
242
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800243 mDisplay = display;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800244 mSurface = surface;
Mathias Agopiana4912602012-07-12 14:25:33 -0700245 mFormat = format;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700246 mPageFlipCount = 0;
Mathias Agopian1f7bec62010-06-25 18:02:21 -0700247
Mathias Agopian98a121a2012-07-24 21:08:59 -0700248 // initialize the display orientation transform.
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700249 DisplayDevice::setOrientation(ISurfaceComposer::eOrientationDefault);
Mathias Agopiana350ff92010-08-10 17:14:02 -0700250}
251
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700252uint32_t DisplayDevice::getPageFlipCount() const {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700253 return mPageFlipCount;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800254}
255
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700256status_t DisplayDevice::compositionComplete() const {
Mathias Agopiana4912602012-07-12 14:25:33 -0700257 if (mFramebufferSurface == NULL) {
258 return NO_ERROR;
259 }
260 return mFramebufferSurface->compositionComplete();
Mathias Agopian74faca22009-09-17 16:18:16 -0700261}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800262
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700263void DisplayDevice::flip(const Region& dirty) const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800264{
265 checkGLErrors();
266
267 EGLDisplay dpy = mDisplay;
268 EGLSurface surface = mSurface;
269
Mathias Agopian5e78e092009-06-11 17:19:54 -0700270#ifdef EGL_ANDROID_swap_rectangle
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700271 if (mFlags & SWAP_RECTANGLE) {
Mathias Agopianb8a55602009-06-26 19:06:36 -0700272 const Region newDirty(dirty.intersect(bounds()));
273 const Rect b(newDirty.getBounds());
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700274 eglSetSwapRectangleANDROID(dpy, surface,
275 b.left, b.top, b.width(), b.height());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800276 }
Mathias Agopian5e78e092009-06-11 17:19:54 -0700277#endif
278
Mathias Agopian95a666b2009-09-24 14:57:26 -0700279 if (mFlags & PARTIAL_UPDATES) {
Mathias Agopiana4912602012-07-12 14:25:33 -0700280 if (mFramebufferSurface != NULL) {
281 mFramebufferSurface->setUpdateRectangle(dirty.getBounds());
282 }
Mathias Agopian1e16b132009-05-07 17:40:23 -0700283 }
284
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700285 mPageFlipCount++;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800286}
287
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700288uint32_t DisplayDevice::getFlags() const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800289{
290 return mFlags;
291}
292
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700293void DisplayDevice::dump(String8& res) const
Erik Gilling1d21a9c2010-12-01 16:38:01 -0800294{
Mathias Agopiana4912602012-07-12 14:25:33 -0700295 if (mFramebufferSurface != NULL) {
296 mFramebufferSurface->dump(res);
297 }
Erik Gilling1d21a9c2010-12-01 16:38:01 -0800298}
Mathias Agopian1b031492012-06-20 17:51:20 -0700299
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700300void DisplayDevice::makeCurrent(const DisplayDevice& hw, EGLContext ctx) {
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700301 EGLSurface sur = eglGetCurrentSurface(EGL_DRAW);
302 if (sur != hw.mSurface) {
303 EGLDisplay dpy = eglGetCurrentDisplay();
304 eglMakeCurrent(dpy, hw.mSurface, hw.mSurface, ctx);
305 }
306}
307
Mathias Agopian1b031492012-06-20 17:51:20 -0700308// ----------------------------------------------------------------------------
309
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700310void DisplayDevice::setVisibleLayersSortedByZ(const Vector< sp<LayerBase> >& layers) {
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700311 mVisibleLayersSortedByZ = layers;
312 size_t count = layers.size();
313 for (size_t i=0 ; i<count ; i++) {
314 if (layers[i]->isSecure()) {
315 mSecureLayerVisible = true;
316 }
317 }
318}
319
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700320Vector< sp<LayerBase> > DisplayDevice::getVisibleLayersSortedByZ() const {
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700321 return mVisibleLayersSortedByZ;
322}
323
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700324bool DisplayDevice::getSecureLayerVisible() const {
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700325 return mSecureLayerVisible;
326}
327
328// ----------------------------------------------------------------------------
329
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700330bool DisplayDevice::canDraw() const {
331 return mScreenAcquired;
332}
333
334void DisplayDevice::releaseScreen() const {
335 mScreenAcquired = false;
336}
337
338void DisplayDevice::acquireScreen() const {
339 mScreenAcquired = true;
340}
341
342bool DisplayDevice::isScreenAcquired() const {
343 return mScreenAcquired;
344}
345
346// ----------------------------------------------------------------------------
347
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700348status_t DisplayDevice::orientationToTransfrom(
Mathias Agopian1b031492012-06-20 17:51:20 -0700349 int orientation, int w, int h, Transform* tr)
350{
351 uint32_t flags = 0;
352 switch (orientation) {
353 case ISurfaceComposer::eOrientationDefault:
354 flags = Transform::ROT_0;
355 break;
356 case ISurfaceComposer::eOrientation90:
357 flags = Transform::ROT_90;
358 break;
359 case ISurfaceComposer::eOrientation180:
360 flags = Transform::ROT_180;
361 break;
362 case ISurfaceComposer::eOrientation270:
363 flags = Transform::ROT_270;
364 break;
365 default:
366 return BAD_VALUE;
367 }
368 tr->set(flags, w, h);
369 return NO_ERROR;
370}
371
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700372status_t DisplayDevice::setOrientation(int orientation) {
Mathias Agopian98a121a2012-07-24 21:08:59 -0700373 int w = mDisplayWidth;
374 int h = mDisplayHeight;
Mathias Agopian1b031492012-06-20 17:51:20 -0700375
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700376 DisplayDevice::orientationToTransfrom(
Mathias Agopian98a121a2012-07-24 21:08:59 -0700377 orientation, w, h, &mGlobalTransform);
Mathias Agopian1b031492012-06-20 17:51:20 -0700378 if (orientation & ISurfaceComposer::eOrientationSwapMask) {
Mathias Agopian98a121a2012-07-24 21:08:59 -0700379 int tmp = w;
380 w = h;
381 h = tmp;
Mathias Agopian1b031492012-06-20 17:51:20 -0700382 }
Mathias Agopian1b031492012-06-20 17:51:20 -0700383 mOrientation = orientation;
Mathias Agopian92a979a2012-08-02 18:32:23 -0700384 dirtyRegion.set(bounds());
Mathias Agopian1b031492012-06-20 17:51:20 -0700385 return NO_ERROR;
386}