blob: 326a1240ed3f2c47cef4ffabd732332d9150ed65 [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
62static __attribute__((noinline))
63void checkEGLErrors(const char* token)
64{
Mathias Agopian870b8aa2012-02-24 16:42:46 -080065 struct EGLUtils {
66 static const char *strerror(EGLint err) {
67 switch (err){
68 case EGL_SUCCESS: return "EGL_SUCCESS";
69 case EGL_NOT_INITIALIZED: return "EGL_NOT_INITIALIZED";
70 case EGL_BAD_ACCESS: return "EGL_BAD_ACCESS";
71 case EGL_BAD_ALLOC: return "EGL_BAD_ALLOC";
72 case EGL_BAD_ATTRIBUTE: return "EGL_BAD_ATTRIBUTE";
73 case EGL_BAD_CONFIG: return "EGL_BAD_CONFIG";
74 case EGL_BAD_CONTEXT: return "EGL_BAD_CONTEXT";
75 case EGL_BAD_CURRENT_SURFACE: return "EGL_BAD_CURRENT_SURFACE";
76 case EGL_BAD_DISPLAY: return "EGL_BAD_DISPLAY";
77 case EGL_BAD_MATCH: return "EGL_BAD_MATCH";
78 case EGL_BAD_NATIVE_PIXMAP: return "EGL_BAD_NATIVE_PIXMAP";
79 case EGL_BAD_NATIVE_WINDOW: return "EGL_BAD_NATIVE_WINDOW";
80 case EGL_BAD_PARAMETER: return "EGL_BAD_PARAMETER";
81 case EGL_BAD_SURFACE: return "EGL_BAD_SURFACE";
82 case EGL_CONTEXT_LOST: return "EGL_CONTEXT_LOST";
83 default: return "UNKNOWN";
84 }
85 }
86 };
87
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080088 EGLint error = eglGetError();
Mathias Agopiancbb288b2009-09-07 16:32:45 -070089 if (error && error != EGL_SUCCESS) {
Steve Blocke6f43dd2012-01-06 19:20:56 +000090 ALOGE("%s: EGL error 0x%04x (%s)",
Mathias Agopian0928e312009-08-07 16:38:10 -070091 token, int(error), EGLUtils::strerror(error));
Mathias Agopiancbb288b2009-09-07 16:32:45 -070092 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080093}
94
Mathias Agopiana4912602012-07-12 14:25:33 -070095// ----------------------------------------------------------------------------
96
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080097/*
98 * Initialize the display to the specified values.
99 *
100 */
101
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700102DisplayDevice::DisplayDevice(
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800103 const sp<SurfaceFlinger>& flinger,
Mathias Agopiana4912602012-07-12 14:25:33 -0700104 int display,
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700105 const sp<ANativeWindow>& nativeWindow,
106 const sp<FramebufferSurface>& framebufferSurface,
Mathias Agopiana4912602012-07-12 14:25:33 -0700107 EGLConfig config)
Mathias Agopian92a979a2012-08-02 18:32:23 -0700108 : mFlinger(flinger),
109 mId(display),
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700110 mNativeWindow(nativeWindow),
111 mFramebufferSurface(framebufferSurface),
Mathias Agopian92a979a2012-08-02 18:32:23 -0700112 mDisplay(EGL_NO_DISPLAY),
113 mSurface(EGL_NO_SURFACE),
114 mContext(EGL_NO_CONTEXT),
115 mDpiX(), mDpiY(),
Mathias Agopian92a979a2012-08-02 18:32:23 -0700116 mDensity(),
117 mDisplayWidth(), mDisplayHeight(), mFormat(),
118 mFlags(),
119 mPageFlipCount(),
Mathias Agopian92a979a2012-08-02 18:32:23 -0700120 mSecureLayerVisible(false),
121 mScreenAcquired(false),
122 mOrientation(),
123 mLayerStack(0)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800124{
Mathias Agopiana4912602012-07-12 14:25:33 -0700125 init(config);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800126}
127
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700128DisplayDevice::~DisplayDevice() {
Mathias Agopian92a979a2012-08-02 18:32:23 -0700129 if (mSurface != EGL_NO_SURFACE) {
130 eglDestroySurface(mDisplay, mSurface);
131 mSurface = EGL_NO_SURFACE;
132 }
133}
134
135bool DisplayDevice::isValid() const {
136 return mFlinger != NULL;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800137}
138
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700139float DisplayDevice::getDpiX() const {
Mathias Agopiana4912602012-07-12 14:25:33 -0700140 return mDpiX;
Mathias Agopian3d64e732011-04-18 15:59:24 -0700141}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800142
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700143float DisplayDevice::getDpiY() const {
Mathias Agopiana4912602012-07-12 14:25:33 -0700144 return mDpiY;
Mathias Agopian61630912011-07-06 16:35:30 -0700145}
146
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700147float DisplayDevice::getDensity() const {
Mathias Agopiana4912602012-07-12 14:25:33 -0700148 return mDensity;
149}
Mathias Agopian61630912011-07-06 16:35:30 -0700150
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700151int DisplayDevice::getWidth() const {
Mathias Agopiana4912602012-07-12 14:25:33 -0700152 return mDisplayWidth;
153}
154
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700155int DisplayDevice::getHeight() const {
Mathias Agopiana4912602012-07-12 14:25:33 -0700156 return mDisplayHeight;
157}
158
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700159PixelFormat DisplayDevice::getFormat() const {
Mathias Agopiana4912602012-07-12 14:25:33 -0700160 return mFormat;
161}
162
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700163EGLSurface DisplayDevice::getEGLSurface() const {
Mathias Agopiana4912602012-07-12 14:25:33 -0700164 return mSurface;
165}
166
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700167void DisplayDevice::init(EGLConfig config)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800168{
Mathias Agopiana4912602012-07-12 14:25:33 -0700169 ANativeWindow* const window = mNativeWindow.get();
170
Mathias Agopian61630912011-07-06 16:35:30 -0700171 int format;
Mathias Agopian61630912011-07-06 16:35:30 -0700172 window->query(window, NATIVE_WINDOW_FORMAT, &format);
Mathias Agopiana4912602012-07-12 14:25:33 -0700173 mDpiX = window->xdpi;
174 mDpiY = window->ydpi;
Mathias Agopiana4912602012-07-12 14:25:33 -0700175
176 // TODO: Not sure if display density should handled by SF any longer
Mathias Agopianb5dd9c02012-03-22 12:15:54 -0700177 class Density {
178 static int getDensityFromProperty(char const* propName) {
179 char property[PROPERTY_VALUE_MAX];
180 int density = 0;
181 if (property_get(propName, property, NULL) > 0) {
182 density = atoi(property);
183 }
184 return density;
185 }
186 public:
187 static int getEmuDensity() {
188 return getDensityFromProperty("qemu.sf.lcd_density"); }
189 static int getBuildDensity() {
190 return getDensityFromProperty("ro.sf.lcd_density"); }
191 };
Mathias Agopianb5dd9c02012-03-22 12:15:54 -0700192 // The density of the device is provided by a build property
193 mDensity = Density::getBuildDensity() / 160.0f;
Mathias Agopianb5dd9c02012-03-22 12:15:54 -0700194 if (mDensity == 0) {
195 // the build doesn't provide a density -- this is wrong!
196 // use xdpi instead
197 ALOGE("ro.sf.lcd_density must be defined as a build property");
198 mDensity = mDpiX / 160.0f;
199 }
Mathias Agopianb5dd9c02012-03-22 12:15:54 -0700200 if (Density::getEmuDensity()) {
201 // if "qemu.sf.lcd_density" is specified, it overrides everything
202 mDpiX = mDpiY = mDensity = Density::getEmuDensity();
203 mDensity /= 160.0f;
204 }
205
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800206 /*
Mathias Agopiana4912602012-07-12 14:25:33 -0700207 * Create our display's surface
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800208 */
209
Mathias Agopiana4912602012-07-12 14:25:33 -0700210 EGLSurface surface;
211 EGLint w, h;
212 EGLDisplay display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
213 surface = eglCreateWindowSurface(display, config, window, NULL);
Mathias Agopian1b031492012-06-20 17:51:20 -0700214 eglQuerySurface(display, surface, EGL_WIDTH, &mDisplayWidth);
215 eglQuerySurface(display, surface, EGL_HEIGHT, &mDisplayHeight);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800216
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800217 mDisplay = display;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800218 mSurface = surface;
Mathias Agopiana4912602012-07-12 14:25:33 -0700219 mFormat = format;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700220 mPageFlipCount = 0;
Mathias Agopian1f7bec62010-06-25 18:02:21 -0700221
Mathias Agopian98a121a2012-07-24 21:08:59 -0700222 // initialize the display orientation transform.
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700223 DisplayDevice::setOrientation(ISurfaceComposer::eOrientationDefault);
Mathias Agopiana350ff92010-08-10 17:14:02 -0700224}
225
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700226uint32_t DisplayDevice::getPageFlipCount() const {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700227 return mPageFlipCount;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800228}
229
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700230status_t DisplayDevice::compositionComplete() const {
Mathias Agopiana4912602012-07-12 14:25:33 -0700231 if (mFramebufferSurface == NULL) {
232 return NO_ERROR;
233 }
234 return mFramebufferSurface->compositionComplete();
Mathias Agopian74faca22009-09-17 16:18:16 -0700235}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800236
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700237void DisplayDevice::flip(const Region& dirty) const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800238{
239 checkGLErrors();
240
241 EGLDisplay dpy = mDisplay;
242 EGLSurface surface = mSurface;
243
Mathias Agopian5e78e092009-06-11 17:19:54 -0700244#ifdef EGL_ANDROID_swap_rectangle
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700245 if (mFlags & SWAP_RECTANGLE) {
Mathias Agopianb8a55602009-06-26 19:06:36 -0700246 const Region newDirty(dirty.intersect(bounds()));
247 const Rect b(newDirty.getBounds());
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700248 eglSetSwapRectangleANDROID(dpy, surface,
249 b.left, b.top, b.width(), b.height());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800250 }
Mathias Agopian5e78e092009-06-11 17:19:54 -0700251#endif
252
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700253 mPageFlipCount++;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800254}
255
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700256uint32_t DisplayDevice::getFlags() const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800257{
258 return mFlags;
259}
260
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700261void DisplayDevice::dump(String8& res) const
Erik Gilling1d21a9c2010-12-01 16:38:01 -0800262{
Mathias Agopiana4912602012-07-12 14:25:33 -0700263 if (mFramebufferSurface != NULL) {
264 mFramebufferSurface->dump(res);
265 }
Erik Gilling1d21a9c2010-12-01 16:38:01 -0800266}
Mathias Agopian1b031492012-06-20 17:51:20 -0700267
Mathias Agopian42977342012-08-05 00:40:46 -0700268void DisplayDevice::makeCurrent(const sp<const DisplayDevice>& hw, EGLContext ctx) {
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700269 EGLSurface sur = eglGetCurrentSurface(EGL_DRAW);
Mathias Agopian42977342012-08-05 00:40:46 -0700270 if (sur != hw->mSurface) {
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700271 EGLDisplay dpy = eglGetCurrentDisplay();
Mathias Agopian42977342012-08-05 00:40:46 -0700272 eglMakeCurrent(dpy, hw->mSurface, hw->mSurface, ctx);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700273 }
274}
275
Mathias Agopian1b031492012-06-20 17:51:20 -0700276// ----------------------------------------------------------------------------
277
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700278void DisplayDevice::setVisibleLayersSortedByZ(const Vector< sp<LayerBase> >& layers) {
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700279 mVisibleLayersSortedByZ = layers;
280 size_t count = layers.size();
281 for (size_t i=0 ; i<count ; i++) {
282 if (layers[i]->isSecure()) {
283 mSecureLayerVisible = true;
284 }
285 }
286}
287
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700288Vector< sp<LayerBase> > DisplayDevice::getVisibleLayersSortedByZ() const {
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700289 return mVisibleLayersSortedByZ;
290}
291
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700292bool DisplayDevice::getSecureLayerVisible() const {
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700293 return mSecureLayerVisible;
294}
295
296// ----------------------------------------------------------------------------
297
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700298bool DisplayDevice::canDraw() const {
299 return mScreenAcquired;
300}
301
302void DisplayDevice::releaseScreen() const {
303 mScreenAcquired = false;
304}
305
306void DisplayDevice::acquireScreen() const {
307 mScreenAcquired = true;
308}
309
310bool DisplayDevice::isScreenAcquired() const {
311 return mScreenAcquired;
312}
313
314// ----------------------------------------------------------------------------
315
Mathias Agopian28947d72012-08-08 18:51:15 -0700316void DisplayDevice::setLayerStack(uint32_t stack) {
317 mLayerStack = stack;
318 dirtyRegion.set(bounds());
319}
320
321// ----------------------------------------------------------------------------
322
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700323status_t DisplayDevice::orientationToTransfrom(
Mathias Agopian1b031492012-06-20 17:51:20 -0700324 int orientation, int w, int h, Transform* tr)
325{
326 uint32_t flags = 0;
327 switch (orientation) {
328 case ISurfaceComposer::eOrientationDefault:
329 flags = Transform::ROT_0;
330 break;
331 case ISurfaceComposer::eOrientation90:
332 flags = Transform::ROT_90;
333 break;
334 case ISurfaceComposer::eOrientation180:
335 flags = Transform::ROT_180;
336 break;
337 case ISurfaceComposer::eOrientation270:
338 flags = Transform::ROT_270;
339 break;
340 default:
341 return BAD_VALUE;
342 }
343 tr->set(flags, w, h);
344 return NO_ERROR;
345}
346
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700347status_t DisplayDevice::setOrientation(int orientation) {
Mathias Agopian98a121a2012-07-24 21:08:59 -0700348 int w = mDisplayWidth;
349 int h = mDisplayHeight;
Mathias Agopian1b031492012-06-20 17:51:20 -0700350
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700351 DisplayDevice::orientationToTransfrom(
Mathias Agopian98a121a2012-07-24 21:08:59 -0700352 orientation, w, h, &mGlobalTransform);
Mathias Agopian1b031492012-06-20 17:51:20 -0700353 if (orientation & ISurfaceComposer::eOrientationSwapMask) {
Mathias Agopian98a121a2012-07-24 21:08:59 -0700354 int tmp = w;
355 w = h;
356 h = tmp;
Mathias Agopian1b031492012-06-20 17:51:20 -0700357 }
Mathias Agopian1b031492012-06-20 17:51:20 -0700358 mOrientation = orientation;
Mathias Agopian92a979a2012-08-02 18:32:23 -0700359 dirtyRegion.set(bounds());
Mathias Agopian1b031492012-06-20 17:51:20 -0700360 return NO_ERROR;
361}