blob: a33b94bd308840b4513f30627cef20e4872a86a2 [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,
111 const sp<SurfaceTextureClient>& surface,
112 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(),
120 mRefreshRate(),
121 mDensity(),
122 mDisplayWidth(), mDisplayHeight(), mFormat(),
123 mFlags(),
124 mPageFlipCount(),
125 mRefreshPeriod(),
126 mSecureLayerVisible(false),
127 mScreenAcquired(false),
128 mOrientation(),
129 mLayerStack(0)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800130{
Mathias Agopiana4912602012-07-12 14:25:33 -0700131 init(config);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800132}
133
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700134DisplayDevice::~DisplayDevice() {
Mathias Agopian92a979a2012-08-02 18:32:23 -0700135 // DO NOT call terminate() from here, because we create
136 // temporaries of this class (on the stack typically), and we don't
137 // want to destroy the EGLSurface in that case
138}
139
140void DisplayDevice::terminate() {
141 if (mSurface != EGL_NO_SURFACE) {
142 eglDestroySurface(mDisplay, mSurface);
143 mSurface = EGL_NO_SURFACE;
144 }
145}
146
147bool DisplayDevice::isValid() const {
148 return mFlinger != NULL;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800149}
150
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700151float DisplayDevice::getDpiX() const {
Mathias Agopiana4912602012-07-12 14:25:33 -0700152 return mDpiX;
Mathias Agopian3d64e732011-04-18 15:59:24 -0700153}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800154
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700155float DisplayDevice::getDpiY() const {
Mathias Agopiana4912602012-07-12 14:25:33 -0700156 return mDpiY;
Mathias Agopian61630912011-07-06 16:35:30 -0700157}
158
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700159float DisplayDevice::getDensity() const {
Mathias Agopiana4912602012-07-12 14:25:33 -0700160 return mDensity;
161}
Mathias Agopian61630912011-07-06 16:35:30 -0700162
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700163float DisplayDevice::getRefreshRate() const {
Mathias Agopiana4912602012-07-12 14:25:33 -0700164 return mRefreshRate;
165}
166
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700167int DisplayDevice::getWidth() const {
Mathias Agopiana4912602012-07-12 14:25:33 -0700168 return mDisplayWidth;
169}
170
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700171int DisplayDevice::getHeight() const {
Mathias Agopiana4912602012-07-12 14:25:33 -0700172 return mDisplayHeight;
173}
174
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700175PixelFormat DisplayDevice::getFormat() const {
Mathias Agopiana4912602012-07-12 14:25:33 -0700176 return mFormat;
177}
178
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700179EGLSurface DisplayDevice::getEGLSurface() const {
Mathias Agopiana4912602012-07-12 14:25:33 -0700180 return mSurface;
181}
182
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700183status_t DisplayDevice::getInfo(DisplayInfo* info) const {
Mathias Agopianc666cae2012-07-25 18:56:13 -0700184 info->w = getWidth();
185 info->h = getHeight();
186 info->xdpi = getDpiX();
187 info->ydpi = getDpiY();
188 info->fps = getRefreshRate();
189 info->density = getDensity();
190 info->orientation = getOrientation();
191 // TODO: this needs to go away (currently needed only by webkit)
192 getPixelFormatInfo(getFormat(), &info->pixelFormatInfo);
193 return NO_ERROR;
194}
195
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700196void DisplayDevice::init(EGLConfig config)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800197{
Mathias Agopiana4912602012-07-12 14:25:33 -0700198 ANativeWindow* const window = mNativeWindow.get();
199
200 int concreteType;
201 window->query(window, NATIVE_WINDOW_CONCRETE_TYPE, &concreteType);
202 if (concreteType == NATIVE_WINDOW_FRAMEBUFFER) {
203 mFramebufferSurface = static_cast<FramebufferSurface *>(mNativeWindow.get());
Mathias Agopian1f339ff2011-07-01 17:08:43 -0700204 }
205
Mathias Agopian61630912011-07-06 16:35:30 -0700206 int format;
Mathias Agopian61630912011-07-06 16:35:30 -0700207 window->query(window, NATIVE_WINDOW_FORMAT, &format);
Mathias Agopiana4912602012-07-12 14:25:33 -0700208 mDpiX = window->xdpi;
209 mDpiY = window->ydpi;
210 if (mFramebufferSurface != NULL) {
211 mRefreshRate = mFramebufferSurface->getRefreshRate();
212 } else {
213 mRefreshRate = 60;
Mathias Agopianb5dd9c02012-03-22 12:15:54 -0700214 }
Mathias Agopiana4912602012-07-12 14:25:33 -0700215 mRefreshPeriod = nsecs_t(1e9 / mRefreshRate);
Mathias Agopian385977f2011-11-04 18:46:11 -0700216
Mathias Agopiana4912602012-07-12 14:25:33 -0700217
218 // TODO: Not sure if display density should handled by SF any longer
Mathias Agopianb5dd9c02012-03-22 12:15:54 -0700219 class Density {
220 static int getDensityFromProperty(char const* propName) {
221 char property[PROPERTY_VALUE_MAX];
222 int density = 0;
223 if (property_get(propName, property, NULL) > 0) {
224 density = atoi(property);
225 }
226 return density;
227 }
228 public:
229 static int getEmuDensity() {
230 return getDensityFromProperty("qemu.sf.lcd_density"); }
231 static int getBuildDensity() {
232 return getDensityFromProperty("ro.sf.lcd_density"); }
233 };
Mathias Agopianb5dd9c02012-03-22 12:15:54 -0700234 // The density of the device is provided by a build property
235 mDensity = Density::getBuildDensity() / 160.0f;
Mathias Agopianb5dd9c02012-03-22 12:15:54 -0700236 if (mDensity == 0) {
237 // the build doesn't provide a density -- this is wrong!
238 // use xdpi instead
239 ALOGE("ro.sf.lcd_density must be defined as a build property");
240 mDensity = mDpiX / 160.0f;
241 }
Mathias Agopianb5dd9c02012-03-22 12:15:54 -0700242 if (Density::getEmuDensity()) {
243 // if "qemu.sf.lcd_density" is specified, it overrides everything
244 mDpiX = mDpiY = mDensity = Density::getEmuDensity();
245 mDensity /= 160.0f;
246 }
247
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800248 /*
Mathias Agopiana4912602012-07-12 14:25:33 -0700249 * Create our display's surface
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800250 */
251
Mathias Agopiana4912602012-07-12 14:25:33 -0700252 EGLSurface surface;
253 EGLint w, h;
254 EGLDisplay display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
255 surface = eglCreateWindowSurface(display, config, window, NULL);
Mathias Agopian1b031492012-06-20 17:51:20 -0700256 eglQuerySurface(display, surface, EGL_WIDTH, &mDisplayWidth);
257 eglQuerySurface(display, surface, EGL_HEIGHT, &mDisplayHeight);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800258
Mathias Agopiana4912602012-07-12 14:25:33 -0700259 if (mFramebufferSurface != NULL) {
260 if (mFramebufferSurface->isUpdateOnDemand()) {
261 mFlags |= PARTIAL_UPDATES;
262 // if we have partial updates, we definitely don't need to
263 // preserve the backbuffer, which may be costly.
264 eglSurfaceAttrib(display, surface,
265 EGL_SWAP_BEHAVIOR, EGL_BUFFER_DESTROYED);
266 }
Mathias Agopian0928bee2009-09-16 20:15:42 -0700267 }
268
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800269 mDisplay = display;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800270 mSurface = surface;
Mathias Agopiana4912602012-07-12 14:25:33 -0700271 mFormat = format;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700272 mPageFlipCount = 0;
Mathias Agopian1f7bec62010-06-25 18:02:21 -0700273
Mathias Agopian98a121a2012-07-24 21:08:59 -0700274 // initialize the display orientation transform.
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700275 DisplayDevice::setOrientation(ISurfaceComposer::eOrientationDefault);
Mathias Agopiana350ff92010-08-10 17:14:02 -0700276}
277
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700278uint32_t DisplayDevice::getPageFlipCount() const {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700279 return mPageFlipCount;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800280}
281
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700282nsecs_t DisplayDevice::getRefreshPeriod() const {
Mathias Agopian82d7ab62012-01-19 18:34:40 -0800283 return mRefreshPeriod;
284}
285
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700286status_t DisplayDevice::compositionComplete() const {
Mathias Agopiana4912602012-07-12 14:25:33 -0700287 if (mFramebufferSurface == NULL) {
288 return NO_ERROR;
289 }
290 return mFramebufferSurface->compositionComplete();
Mathias Agopian74faca22009-09-17 16:18:16 -0700291}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800292
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700293void DisplayDevice::flip(const Region& dirty) const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800294{
295 checkGLErrors();
296
297 EGLDisplay dpy = mDisplay;
298 EGLSurface surface = mSurface;
299
Mathias Agopian5e78e092009-06-11 17:19:54 -0700300#ifdef EGL_ANDROID_swap_rectangle
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700301 if (mFlags & SWAP_RECTANGLE) {
Mathias Agopianb8a55602009-06-26 19:06:36 -0700302 const Region newDirty(dirty.intersect(bounds()));
303 const Rect b(newDirty.getBounds());
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700304 eglSetSwapRectangleANDROID(dpy, surface,
305 b.left, b.top, b.width(), b.height());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800306 }
Mathias Agopian5e78e092009-06-11 17:19:54 -0700307#endif
308
Mathias Agopian95a666b2009-09-24 14:57:26 -0700309 if (mFlags & PARTIAL_UPDATES) {
Mathias Agopiana4912602012-07-12 14:25:33 -0700310 if (mFramebufferSurface != NULL) {
311 mFramebufferSurface->setUpdateRectangle(dirty.getBounds());
312 }
Mathias Agopian1e16b132009-05-07 17:40:23 -0700313 }
314
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700315 mPageFlipCount++;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800316}
317
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700318uint32_t DisplayDevice::getFlags() const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800319{
320 return mFlags;
321}
322
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700323void DisplayDevice::dump(String8& res) const
Erik Gilling1d21a9c2010-12-01 16:38:01 -0800324{
Mathias Agopiana4912602012-07-12 14:25:33 -0700325 if (mFramebufferSurface != NULL) {
326 mFramebufferSurface->dump(res);
327 }
Erik Gilling1d21a9c2010-12-01 16:38:01 -0800328}
Mathias Agopian1b031492012-06-20 17:51:20 -0700329
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700330void DisplayDevice::makeCurrent(const DisplayDevice& hw, EGLContext ctx) {
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700331 EGLSurface sur = eglGetCurrentSurface(EGL_DRAW);
332 if (sur != hw.mSurface) {
333 EGLDisplay dpy = eglGetCurrentDisplay();
334 eglMakeCurrent(dpy, hw.mSurface, hw.mSurface, ctx);
335 }
336}
337
Mathias Agopian1b031492012-06-20 17:51:20 -0700338// ----------------------------------------------------------------------------
339
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700340void DisplayDevice::setVisibleLayersSortedByZ(const Vector< sp<LayerBase> >& layers) {
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700341 mVisibleLayersSortedByZ = layers;
342 size_t count = layers.size();
343 for (size_t i=0 ; i<count ; i++) {
344 if (layers[i]->isSecure()) {
345 mSecureLayerVisible = true;
346 }
347 }
348}
349
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700350Vector< sp<LayerBase> > DisplayDevice::getVisibleLayersSortedByZ() const {
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700351 return mVisibleLayersSortedByZ;
352}
353
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700354bool DisplayDevice::getSecureLayerVisible() const {
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700355 return mSecureLayerVisible;
356}
357
358// ----------------------------------------------------------------------------
359
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700360bool DisplayDevice::canDraw() const {
361 return mScreenAcquired;
362}
363
364void DisplayDevice::releaseScreen() const {
365 mScreenAcquired = false;
366}
367
368void DisplayDevice::acquireScreen() const {
369 mScreenAcquired = true;
370}
371
372bool DisplayDevice::isScreenAcquired() const {
373 return mScreenAcquired;
374}
375
376// ----------------------------------------------------------------------------
377
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700378status_t DisplayDevice::orientationToTransfrom(
Mathias Agopian1b031492012-06-20 17:51:20 -0700379 int orientation, int w, int h, Transform* tr)
380{
381 uint32_t flags = 0;
382 switch (orientation) {
383 case ISurfaceComposer::eOrientationDefault:
384 flags = Transform::ROT_0;
385 break;
386 case ISurfaceComposer::eOrientation90:
387 flags = Transform::ROT_90;
388 break;
389 case ISurfaceComposer::eOrientation180:
390 flags = Transform::ROT_180;
391 break;
392 case ISurfaceComposer::eOrientation270:
393 flags = Transform::ROT_270;
394 break;
395 default:
396 return BAD_VALUE;
397 }
398 tr->set(flags, w, h);
399 return NO_ERROR;
400}
401
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700402status_t DisplayDevice::setOrientation(int orientation) {
Mathias Agopian98a121a2012-07-24 21:08:59 -0700403 int w = mDisplayWidth;
404 int h = mDisplayHeight;
Mathias Agopian1b031492012-06-20 17:51:20 -0700405
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700406 DisplayDevice::orientationToTransfrom(
Mathias Agopian98a121a2012-07-24 21:08:59 -0700407 orientation, w, h, &mGlobalTransform);
Mathias Agopian1b031492012-06-20 17:51:20 -0700408 if (orientation & ISurfaceComposer::eOrientationSwapMask) {
Mathias Agopian98a121a2012-07-24 21:08:59 -0700409 int tmp = w;
410 w = h;
411 h = tmp;
Mathias Agopian1b031492012-06-20 17:51:20 -0700412 }
Mathias Agopian1b031492012-06-20 17:51:20 -0700413 mOrientation = orientation;
Mathias Agopian92a979a2012-08-02 18:32:23 -0700414 dirtyRegion.set(bounds());
Mathias Agopian1b031492012-06-20 17:51:20 -0700415 return NO_ERROR;
416}