blob: 175f80852ce68e0432e5fb766e7ce51aa7549bab [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 Agopian0f2f5ff2012-07-31 23:09:07 -0700100DisplayDevice::DisplayDevice(
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800101 const sp<SurfaceFlinger>& flinger,
Mathias Agopiana4912602012-07-12 14:25:33 -0700102 int display,
Mathias Agopiand8552d72012-08-04 21:39:11 -0700103 const sp<ANativeWindow>& surface,
Mathias Agopiana4912602012-07-12 14:25:33 -0700104 EGLConfig config)
Mathias Agopian92a979a2012-08-02 18:32:23 -0700105 : mFlinger(flinger),
106 mId(display),
107 mNativeWindow(surface),
108 mDisplay(EGL_NO_DISPLAY),
109 mSurface(EGL_NO_SURFACE),
110 mContext(EGL_NO_CONTEXT),
111 mDpiX(), mDpiY(),
Mathias Agopian92a979a2012-08-02 18:32:23 -0700112 mDensity(),
113 mDisplayWidth(), mDisplayHeight(), mFormat(),
114 mFlags(),
115 mPageFlipCount(),
Mathias Agopian92a979a2012-08-02 18:32:23 -0700116 mSecureLayerVisible(false),
117 mScreenAcquired(false),
118 mOrientation(),
119 mLayerStack(0)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800120{
Mathias Agopiana4912602012-07-12 14:25:33 -0700121 init(config);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800122}
123
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700124DisplayDevice::~DisplayDevice() {
Mathias Agopian92a979a2012-08-02 18:32:23 -0700125 if (mSurface != EGL_NO_SURFACE) {
126 eglDestroySurface(mDisplay, mSurface);
127 mSurface = EGL_NO_SURFACE;
128 }
129}
130
131bool DisplayDevice::isValid() const {
132 return mFlinger != NULL;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800133}
134
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700135float DisplayDevice::getDpiX() const {
Mathias Agopiana4912602012-07-12 14:25:33 -0700136 return mDpiX;
Mathias Agopian3d64e732011-04-18 15:59:24 -0700137}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800138
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700139float DisplayDevice::getDpiY() const {
Mathias Agopiana4912602012-07-12 14:25:33 -0700140 return mDpiY;
Mathias Agopian61630912011-07-06 16:35:30 -0700141}
142
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700143float DisplayDevice::getDensity() const {
Mathias Agopiana4912602012-07-12 14:25:33 -0700144 return mDensity;
145}
Mathias Agopian61630912011-07-06 16:35:30 -0700146
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700147int DisplayDevice::getWidth() const {
Mathias Agopiana4912602012-07-12 14:25:33 -0700148 return mDisplayWidth;
149}
150
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700151int DisplayDevice::getHeight() const {
Mathias Agopiana4912602012-07-12 14:25:33 -0700152 return mDisplayHeight;
153}
154
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700155PixelFormat DisplayDevice::getFormat() const {
Mathias Agopiana4912602012-07-12 14:25:33 -0700156 return mFormat;
157}
158
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700159EGLSurface DisplayDevice::getEGLSurface() const {
Mathias Agopiana4912602012-07-12 14:25:33 -0700160 return mSurface;
161}
162
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700163void DisplayDevice::init(EGLConfig config)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800164{
Mathias Agopiana4912602012-07-12 14:25:33 -0700165 ANativeWindow* const window = mNativeWindow.get();
166
167 int concreteType;
168 window->query(window, NATIVE_WINDOW_CONCRETE_TYPE, &concreteType);
169 if (concreteType == NATIVE_WINDOW_FRAMEBUFFER) {
170 mFramebufferSurface = static_cast<FramebufferSurface *>(mNativeWindow.get());
Mathias Agopian1f339ff2011-07-01 17:08:43 -0700171 }
172
Mathias Agopian61630912011-07-06 16:35:30 -0700173 int format;
Mathias Agopian61630912011-07-06 16:35:30 -0700174 window->query(window, NATIVE_WINDOW_FORMAT, &format);
Mathias Agopiana4912602012-07-12 14:25:33 -0700175 mDpiX = window->xdpi;
176 mDpiY = window->ydpi;
Mathias Agopiana4912602012-07-12 14:25:33 -0700177
178 // TODO: Not sure if display density should handled by SF any longer
Mathias Agopianb5dd9c02012-03-22 12:15:54 -0700179 class Density {
180 static int getDensityFromProperty(char const* propName) {
181 char property[PROPERTY_VALUE_MAX];
182 int density = 0;
183 if (property_get(propName, property, NULL) > 0) {
184 density = atoi(property);
185 }
186 return density;
187 }
188 public:
189 static int getEmuDensity() {
190 return getDensityFromProperty("qemu.sf.lcd_density"); }
191 static int getBuildDensity() {
192 return getDensityFromProperty("ro.sf.lcd_density"); }
193 };
Mathias Agopianb5dd9c02012-03-22 12:15:54 -0700194 // The density of the device is provided by a build property
195 mDensity = Density::getBuildDensity() / 160.0f;
Mathias Agopianb5dd9c02012-03-22 12:15:54 -0700196 if (mDensity == 0) {
197 // the build doesn't provide a density -- this is wrong!
198 // use xdpi instead
199 ALOGE("ro.sf.lcd_density must be defined as a build property");
200 mDensity = mDpiX / 160.0f;
201 }
Mathias Agopianb5dd9c02012-03-22 12:15:54 -0700202 if (Density::getEmuDensity()) {
203 // if "qemu.sf.lcd_density" is specified, it overrides everything
204 mDpiX = mDpiY = mDensity = Density::getEmuDensity();
205 mDensity /= 160.0f;
206 }
207
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800208 /*
Mathias Agopiana4912602012-07-12 14:25:33 -0700209 * Create our display's surface
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800210 */
211
Mathias Agopiana4912602012-07-12 14:25:33 -0700212 EGLSurface surface;
213 EGLint w, h;
214 EGLDisplay display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
215 surface = eglCreateWindowSurface(display, config, window, NULL);
Mathias Agopian1b031492012-06-20 17:51:20 -0700216 eglQuerySurface(display, surface, EGL_WIDTH, &mDisplayWidth);
217 eglQuerySurface(display, surface, EGL_HEIGHT, &mDisplayHeight);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800218
Mathias Agopiana4912602012-07-12 14:25:33 -0700219 if (mFramebufferSurface != NULL) {
220 if (mFramebufferSurface->isUpdateOnDemand()) {
221 mFlags |= PARTIAL_UPDATES;
222 // if we have partial updates, we definitely don't need to
223 // preserve the backbuffer, which may be costly.
224 eglSurfaceAttrib(display, surface,
225 EGL_SWAP_BEHAVIOR, EGL_BUFFER_DESTROYED);
226 }
Mathias Agopian0928bee2009-09-16 20:15:42 -0700227 }
228
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800229 mDisplay = display;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800230 mSurface = surface;
Mathias Agopiana4912602012-07-12 14:25:33 -0700231 mFormat = format;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700232 mPageFlipCount = 0;
Mathias Agopian1f7bec62010-06-25 18:02:21 -0700233
Mathias Agopian98a121a2012-07-24 21:08:59 -0700234 // initialize the display orientation transform.
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700235 DisplayDevice::setOrientation(ISurfaceComposer::eOrientationDefault);
Mathias Agopiana350ff92010-08-10 17:14:02 -0700236}
237
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700238uint32_t DisplayDevice::getPageFlipCount() const {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700239 return mPageFlipCount;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800240}
241
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700242status_t DisplayDevice::compositionComplete() const {
Mathias Agopiana4912602012-07-12 14:25:33 -0700243 if (mFramebufferSurface == NULL) {
244 return NO_ERROR;
245 }
246 return mFramebufferSurface->compositionComplete();
Mathias Agopian74faca22009-09-17 16:18:16 -0700247}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800248
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700249void DisplayDevice::flip(const Region& dirty) const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800250{
251 checkGLErrors();
252
253 EGLDisplay dpy = mDisplay;
254 EGLSurface surface = mSurface;
255
Mathias Agopian5e78e092009-06-11 17:19:54 -0700256#ifdef EGL_ANDROID_swap_rectangle
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700257 if (mFlags & SWAP_RECTANGLE) {
Mathias Agopianb8a55602009-06-26 19:06:36 -0700258 const Region newDirty(dirty.intersect(bounds()));
259 const Rect b(newDirty.getBounds());
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700260 eglSetSwapRectangleANDROID(dpy, surface,
261 b.left, b.top, b.width(), b.height());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800262 }
Mathias Agopian5e78e092009-06-11 17:19:54 -0700263#endif
264
Mathias Agopian95a666b2009-09-24 14:57:26 -0700265 if (mFlags & PARTIAL_UPDATES) {
Mathias Agopiana4912602012-07-12 14:25:33 -0700266 if (mFramebufferSurface != NULL) {
267 mFramebufferSurface->setUpdateRectangle(dirty.getBounds());
268 }
Mathias Agopian1e16b132009-05-07 17:40:23 -0700269 }
270
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700271 mPageFlipCount++;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800272}
273
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700274uint32_t DisplayDevice::getFlags() const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800275{
276 return mFlags;
277}
278
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700279void DisplayDevice::dump(String8& res) const
Erik Gilling1d21a9c2010-12-01 16:38:01 -0800280{
Mathias Agopiana4912602012-07-12 14:25:33 -0700281 if (mFramebufferSurface != NULL) {
282 mFramebufferSurface->dump(res);
283 }
Erik Gilling1d21a9c2010-12-01 16:38:01 -0800284}
Mathias Agopian1b031492012-06-20 17:51:20 -0700285
Mathias Agopian42977342012-08-05 00:40:46 -0700286void DisplayDevice::makeCurrent(const sp<const DisplayDevice>& hw, EGLContext ctx) {
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700287 EGLSurface sur = eglGetCurrentSurface(EGL_DRAW);
Mathias Agopian42977342012-08-05 00:40:46 -0700288 if (sur != hw->mSurface) {
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700289 EGLDisplay dpy = eglGetCurrentDisplay();
Mathias Agopian42977342012-08-05 00:40:46 -0700290 eglMakeCurrent(dpy, hw->mSurface, hw->mSurface, ctx);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700291 }
292}
293
Mathias Agopian1b031492012-06-20 17:51:20 -0700294// ----------------------------------------------------------------------------
295
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700296void DisplayDevice::setVisibleLayersSortedByZ(const Vector< sp<LayerBase> >& layers) {
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700297 mVisibleLayersSortedByZ = layers;
298 size_t count = layers.size();
299 for (size_t i=0 ; i<count ; i++) {
300 if (layers[i]->isSecure()) {
301 mSecureLayerVisible = true;
302 }
303 }
304}
305
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700306Vector< sp<LayerBase> > DisplayDevice::getVisibleLayersSortedByZ() const {
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700307 return mVisibleLayersSortedByZ;
308}
309
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700310bool DisplayDevice::getSecureLayerVisible() const {
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700311 return mSecureLayerVisible;
312}
313
314// ----------------------------------------------------------------------------
315
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700316bool DisplayDevice::canDraw() const {
317 return mScreenAcquired;
318}
319
320void DisplayDevice::releaseScreen() const {
321 mScreenAcquired = false;
322}
323
324void DisplayDevice::acquireScreen() const {
325 mScreenAcquired = true;
326}
327
328bool DisplayDevice::isScreenAcquired() const {
329 return mScreenAcquired;
330}
331
332// ----------------------------------------------------------------------------
333
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700334status_t DisplayDevice::orientationToTransfrom(
Mathias Agopian1b031492012-06-20 17:51:20 -0700335 int orientation, int w, int h, Transform* tr)
336{
337 uint32_t flags = 0;
338 switch (orientation) {
339 case ISurfaceComposer::eOrientationDefault:
340 flags = Transform::ROT_0;
341 break;
342 case ISurfaceComposer::eOrientation90:
343 flags = Transform::ROT_90;
344 break;
345 case ISurfaceComposer::eOrientation180:
346 flags = Transform::ROT_180;
347 break;
348 case ISurfaceComposer::eOrientation270:
349 flags = Transform::ROT_270;
350 break;
351 default:
352 return BAD_VALUE;
353 }
354 tr->set(flags, w, h);
355 return NO_ERROR;
356}
357
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700358status_t DisplayDevice::setOrientation(int orientation) {
Mathias Agopian98a121a2012-07-24 21:08:59 -0700359 int w = mDisplayWidth;
360 int h = mDisplayHeight;
Mathias Agopian1b031492012-06-20 17:51:20 -0700361
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700362 DisplayDevice::orientationToTransfrom(
Mathias Agopian98a121a2012-07-24 21:08:59 -0700363 orientation, w, h, &mGlobalTransform);
Mathias Agopian1b031492012-06-20 17:51:20 -0700364 if (orientation & ISurfaceComposer::eOrientationSwapMask) {
Mathias Agopian98a121a2012-07-24 21:08:59 -0700365 int tmp = w;
366 w = h;
367 h = tmp;
Mathias Agopian1b031492012-06-20 17:51:20 -0700368 }
Mathias Agopian1b031492012-06-20 17:51:20 -0700369 mOrientation = orientation;
Mathias Agopian92a979a2012-08-02 18:32:23 -0700370 dirtyRegion.set(bounds());
Mathias Agopian1b031492012-06-20 17:51:20 -0700371 return NO_ERROR;
372}