blob: b09b77c422e2214cf18d2ac9baab0e19c9fff509 [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,
103 const sp<SurfaceTextureClient>& surface,
104 EGLConfig config)
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700105 : mFlinger(flinger),
106 mDisplayId(display),
107 mNativeWindow(surface),
108 mDisplay(EGL_NO_DISPLAY),
109 mSurface(EGL_NO_SURFACE),
110 mContext(EGL_NO_CONTEXT),
111 mDpiX(), mDpiY(),
112 mRefreshRate(),
113 mDensity(),
114 mDisplayWidth(), mDisplayHeight(), mFormat(),
115 mFlags(),
116 mPageFlipCount(),
117 mRefreshPeriod(),
118 mSecureLayerVisible(false),
119 mScreenAcquired(false),
120 mOrientation(),
121 mLayerStack(0)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800122{
Mathias Agopiana4912602012-07-12 14:25:33 -0700123 init(config);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800124}
125
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700126DisplayDevice::~DisplayDevice() {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800127}
128
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700129float DisplayDevice::getDpiX() const {
Mathias Agopiana4912602012-07-12 14:25:33 -0700130 return mDpiX;
Mathias Agopian3d64e732011-04-18 15:59:24 -0700131}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800132
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700133float DisplayDevice::getDpiY() const {
Mathias Agopiana4912602012-07-12 14:25:33 -0700134 return mDpiY;
Mathias Agopian61630912011-07-06 16:35:30 -0700135}
136
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700137float DisplayDevice::getDensity() const {
Mathias Agopiana4912602012-07-12 14:25:33 -0700138 return mDensity;
139}
Mathias Agopian61630912011-07-06 16:35:30 -0700140
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700141float DisplayDevice::getRefreshRate() const {
Mathias Agopiana4912602012-07-12 14:25:33 -0700142 return mRefreshRate;
143}
144
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700145int DisplayDevice::getWidth() const {
Mathias Agopiana4912602012-07-12 14:25:33 -0700146 return mDisplayWidth;
147}
148
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700149int DisplayDevice::getHeight() const {
Mathias Agopiana4912602012-07-12 14:25:33 -0700150 return mDisplayHeight;
151}
152
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700153PixelFormat DisplayDevice::getFormat() const {
Mathias Agopiana4912602012-07-12 14:25:33 -0700154 return mFormat;
155}
156
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700157EGLSurface DisplayDevice::getEGLSurface() const {
Mathias Agopiana4912602012-07-12 14:25:33 -0700158 return mSurface;
159}
160
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700161status_t DisplayDevice::getInfo(DisplayInfo* info) const {
Mathias Agopianc666cae2012-07-25 18:56:13 -0700162 info->w = getWidth();
163 info->h = getHeight();
164 info->xdpi = getDpiX();
165 info->ydpi = getDpiY();
166 info->fps = getRefreshRate();
167 info->density = getDensity();
168 info->orientation = getOrientation();
169 // TODO: this needs to go away (currently needed only by webkit)
170 getPixelFormatInfo(getFormat(), &info->pixelFormatInfo);
171 return NO_ERROR;
172}
173
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700174void DisplayDevice::init(EGLConfig config)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800175{
Mathias Agopiana4912602012-07-12 14:25:33 -0700176 ANativeWindow* const window = mNativeWindow.get();
177
178 int concreteType;
179 window->query(window, NATIVE_WINDOW_CONCRETE_TYPE, &concreteType);
180 if (concreteType == NATIVE_WINDOW_FRAMEBUFFER) {
181 mFramebufferSurface = static_cast<FramebufferSurface *>(mNativeWindow.get());
Mathias Agopian1f339ff2011-07-01 17:08:43 -0700182 }
183
Mathias Agopian61630912011-07-06 16:35:30 -0700184 int format;
Mathias Agopian61630912011-07-06 16:35:30 -0700185 window->query(window, NATIVE_WINDOW_FORMAT, &format);
Mathias Agopiana4912602012-07-12 14:25:33 -0700186 mDpiX = window->xdpi;
187 mDpiY = window->ydpi;
188 if (mFramebufferSurface != NULL) {
189 mRefreshRate = mFramebufferSurface->getRefreshRate();
190 } else {
191 mRefreshRate = 60;
Mathias Agopianb5dd9c02012-03-22 12:15:54 -0700192 }
Mathias Agopiana4912602012-07-12 14:25:33 -0700193 mRefreshPeriod = nsecs_t(1e9 / mRefreshRate);
Mathias Agopian385977f2011-11-04 18:46:11 -0700194
Mathias Agopiana4912602012-07-12 14:25:33 -0700195
196 // TODO: Not sure if display density should handled by SF any longer
Mathias Agopianb5dd9c02012-03-22 12:15:54 -0700197 class Density {
198 static int getDensityFromProperty(char const* propName) {
199 char property[PROPERTY_VALUE_MAX];
200 int density = 0;
201 if (property_get(propName, property, NULL) > 0) {
202 density = atoi(property);
203 }
204 return density;
205 }
206 public:
207 static int getEmuDensity() {
208 return getDensityFromProperty("qemu.sf.lcd_density"); }
209 static int getBuildDensity() {
210 return getDensityFromProperty("ro.sf.lcd_density"); }
211 };
Mathias Agopianb5dd9c02012-03-22 12:15:54 -0700212 // The density of the device is provided by a build property
213 mDensity = Density::getBuildDensity() / 160.0f;
Mathias Agopianb5dd9c02012-03-22 12:15:54 -0700214 if (mDensity == 0) {
215 // the build doesn't provide a density -- this is wrong!
216 // use xdpi instead
217 ALOGE("ro.sf.lcd_density must be defined as a build property");
218 mDensity = mDpiX / 160.0f;
219 }
Mathias Agopianb5dd9c02012-03-22 12:15:54 -0700220 if (Density::getEmuDensity()) {
221 // if "qemu.sf.lcd_density" is specified, it overrides everything
222 mDpiX = mDpiY = mDensity = Density::getEmuDensity();
223 mDensity /= 160.0f;
224 }
225
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800226 /*
Mathias Agopiana4912602012-07-12 14:25:33 -0700227 * Create our display's surface
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800228 */
229
Mathias Agopiana4912602012-07-12 14:25:33 -0700230 EGLSurface surface;
231 EGLint w, h;
232 EGLDisplay display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
233 surface = eglCreateWindowSurface(display, config, window, NULL);
Mathias Agopian1b031492012-06-20 17:51:20 -0700234 eglQuerySurface(display, surface, EGL_WIDTH, &mDisplayWidth);
235 eglQuerySurface(display, surface, EGL_HEIGHT, &mDisplayHeight);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800236
Mathias Agopiana4912602012-07-12 14:25:33 -0700237 if (mFramebufferSurface != NULL) {
238 if (mFramebufferSurface->isUpdateOnDemand()) {
239 mFlags |= PARTIAL_UPDATES;
240 // if we have partial updates, we definitely don't need to
241 // preserve the backbuffer, which may be costly.
242 eglSurfaceAttrib(display, surface,
243 EGL_SWAP_BEHAVIOR, EGL_BUFFER_DESTROYED);
244 }
Mathias Agopian0928bee2009-09-16 20:15:42 -0700245 }
246
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800247 mDisplay = display;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800248 mSurface = surface;
Mathias Agopiana4912602012-07-12 14:25:33 -0700249 mFormat = format;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700250 mPageFlipCount = 0;
Mathias Agopian1f7bec62010-06-25 18:02:21 -0700251
Mathias Agopian98a121a2012-07-24 21:08:59 -0700252 // initialize the display orientation transform.
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700253 DisplayDevice::setOrientation(ISurfaceComposer::eOrientationDefault);
Mathias Agopiana350ff92010-08-10 17:14:02 -0700254}
255
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700256uint32_t DisplayDevice::getPageFlipCount() const {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700257 return mPageFlipCount;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800258}
259
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700260nsecs_t DisplayDevice::getRefreshPeriod() const {
Mathias Agopian82d7ab62012-01-19 18:34:40 -0800261 return mRefreshPeriod;
262}
263
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700264status_t DisplayDevice::compositionComplete() const {
Mathias Agopiana4912602012-07-12 14:25:33 -0700265 if (mFramebufferSurface == NULL) {
266 return NO_ERROR;
267 }
268 return mFramebufferSurface->compositionComplete();
Mathias Agopian74faca22009-09-17 16:18:16 -0700269}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800270
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700271void DisplayDevice::flip(const Region& dirty) const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800272{
273 checkGLErrors();
274
275 EGLDisplay dpy = mDisplay;
276 EGLSurface surface = mSurface;
277
Mathias Agopian5e78e092009-06-11 17:19:54 -0700278#ifdef EGL_ANDROID_swap_rectangle
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700279 if (mFlags & SWAP_RECTANGLE) {
Mathias Agopianb8a55602009-06-26 19:06:36 -0700280 const Region newDirty(dirty.intersect(bounds()));
281 const Rect b(newDirty.getBounds());
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700282 eglSetSwapRectangleANDROID(dpy, surface,
283 b.left, b.top, b.width(), b.height());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800284 }
Mathias Agopian5e78e092009-06-11 17:19:54 -0700285#endif
286
Mathias Agopian95a666b2009-09-24 14:57:26 -0700287 if (mFlags & PARTIAL_UPDATES) {
Mathias Agopiana4912602012-07-12 14:25:33 -0700288 if (mFramebufferSurface != NULL) {
289 mFramebufferSurface->setUpdateRectangle(dirty.getBounds());
290 }
Mathias Agopian1e16b132009-05-07 17:40:23 -0700291 }
292
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700293 mPageFlipCount++;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800294}
295
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700296uint32_t DisplayDevice::getFlags() const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800297{
298 return mFlags;
299}
300
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700301void DisplayDevice::dump(String8& res) const
Erik Gilling1d21a9c2010-12-01 16:38:01 -0800302{
Mathias Agopiana4912602012-07-12 14:25:33 -0700303 if (mFramebufferSurface != NULL) {
304 mFramebufferSurface->dump(res);
305 }
Erik Gilling1d21a9c2010-12-01 16:38:01 -0800306}
Mathias Agopian1b031492012-06-20 17:51:20 -0700307
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700308void DisplayDevice::makeCurrent(const DisplayDevice& hw, EGLContext ctx) {
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700309 EGLSurface sur = eglGetCurrentSurface(EGL_DRAW);
310 if (sur != hw.mSurface) {
311 EGLDisplay dpy = eglGetCurrentDisplay();
312 eglMakeCurrent(dpy, hw.mSurface, hw.mSurface, ctx);
313 }
314}
315
Mathias Agopian1b031492012-06-20 17:51:20 -0700316// ----------------------------------------------------------------------------
317
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700318void DisplayDevice::setVisibleLayersSortedByZ(const Vector< sp<LayerBase> >& layers) {
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700319 mVisibleLayersSortedByZ = layers;
320 size_t count = layers.size();
321 for (size_t i=0 ; i<count ; i++) {
322 if (layers[i]->isSecure()) {
323 mSecureLayerVisible = true;
324 }
325 }
326}
327
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700328Vector< sp<LayerBase> > DisplayDevice::getVisibleLayersSortedByZ() const {
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700329 return mVisibleLayersSortedByZ;
330}
331
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700332bool DisplayDevice::getSecureLayerVisible() const {
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700333 return mSecureLayerVisible;
334}
335
336// ----------------------------------------------------------------------------
337
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700338bool DisplayDevice::canDraw() const {
339 return mScreenAcquired;
340}
341
342void DisplayDevice::releaseScreen() const {
343 mScreenAcquired = false;
344}
345
346void DisplayDevice::acquireScreen() const {
347 mScreenAcquired = true;
348}
349
350bool DisplayDevice::isScreenAcquired() const {
351 return mScreenAcquired;
352}
353
354// ----------------------------------------------------------------------------
355
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700356status_t DisplayDevice::orientationToTransfrom(
Mathias Agopian1b031492012-06-20 17:51:20 -0700357 int orientation, int w, int h, Transform* tr)
358{
359 uint32_t flags = 0;
360 switch (orientation) {
361 case ISurfaceComposer::eOrientationDefault:
362 flags = Transform::ROT_0;
363 break;
364 case ISurfaceComposer::eOrientation90:
365 flags = Transform::ROT_90;
366 break;
367 case ISurfaceComposer::eOrientation180:
368 flags = Transform::ROT_180;
369 break;
370 case ISurfaceComposer::eOrientation270:
371 flags = Transform::ROT_270;
372 break;
373 default:
374 return BAD_VALUE;
375 }
376 tr->set(flags, w, h);
377 return NO_ERROR;
378}
379
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700380status_t DisplayDevice::setOrientation(int orientation) {
Mathias Agopian98a121a2012-07-24 21:08:59 -0700381 int w = mDisplayWidth;
382 int h = mDisplayHeight;
Mathias Agopian1b031492012-06-20 17:51:20 -0700383
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700384 DisplayDevice::orientationToTransfrom(
Mathias Agopian98a121a2012-07-24 21:08:59 -0700385 orientation, w, h, &mGlobalTransform);
Mathias Agopian1b031492012-06-20 17:51:20 -0700386 if (orientation & ISurfaceComposer::eOrientationSwapMask) {
Mathias Agopian98a121a2012-07-24 21:08:59 -0700387 int tmp = w;
388 w = h;
389 h = tmp;
Mathias Agopian1b031492012-06-20 17:51:20 -0700390 }
Mathias Agopian1b031492012-06-20 17:51:20 -0700391 mOrientation = orientation;
Mathias Agopian1b031492012-06-20 17:51:20 -0700392 return NO_ERROR;
393}