blob: 4b5d8abdd8d8a94c575466262195769aeb96b309 [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"
37#include "DisplayHardware/DisplayHardwareBase.h"
38#include "DisplayHardware/HWComposer.h"
39
40#include "DisplayHardware.h"
Mathias Agopian1f7bec62010-06-25 18:02:21 -070041#include "GLExtensions.h"
Mathias Agopianc7d14e22011-08-01 16:32:21 -070042#include "SurfaceFlinger.h"
Mathias Agopian921e6ac2012-07-23 23:11:29 -070043#include "LayerBase.h"
Mathias Agopian1f7bec62010-06-25 18:02:21 -070044
Mathias Agopiana4912602012-07-12 14:25:33 -070045// ----------------------------------------------------------------------------
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080046using namespace android;
Mathias Agopiana4912602012-07-12 14:25:33 -070047// ----------------------------------------------------------------------------
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080048
49static __attribute__((noinline))
50void checkGLErrors()
51{
Mathias Agopiancbb288b2009-09-07 16:32:45 -070052 do {
53 // there could be more than one error flag
54 GLenum error = glGetError();
55 if (error == GL_NO_ERROR)
56 break;
Steve Blocke6f43dd2012-01-06 19:20:56 +000057 ALOGE("GL error 0x%04x", int(error));
Mathias Agopiancbb288b2009-09-07 16:32:45 -070058 } while(true);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080059}
60
61static __attribute__((noinline))
62void checkEGLErrors(const char* token)
63{
Mathias Agopian870b8aa2012-02-24 16:42:46 -080064 struct EGLUtils {
65 static const char *strerror(EGLint err) {
66 switch (err){
67 case EGL_SUCCESS: return "EGL_SUCCESS";
68 case EGL_NOT_INITIALIZED: return "EGL_NOT_INITIALIZED";
69 case EGL_BAD_ACCESS: return "EGL_BAD_ACCESS";
70 case EGL_BAD_ALLOC: return "EGL_BAD_ALLOC";
71 case EGL_BAD_ATTRIBUTE: return "EGL_BAD_ATTRIBUTE";
72 case EGL_BAD_CONFIG: return "EGL_BAD_CONFIG";
73 case EGL_BAD_CONTEXT: return "EGL_BAD_CONTEXT";
74 case EGL_BAD_CURRENT_SURFACE: return "EGL_BAD_CURRENT_SURFACE";
75 case EGL_BAD_DISPLAY: return "EGL_BAD_DISPLAY";
76 case EGL_BAD_MATCH: return "EGL_BAD_MATCH";
77 case EGL_BAD_NATIVE_PIXMAP: return "EGL_BAD_NATIVE_PIXMAP";
78 case EGL_BAD_NATIVE_WINDOW: return "EGL_BAD_NATIVE_WINDOW";
79 case EGL_BAD_PARAMETER: return "EGL_BAD_PARAMETER";
80 case EGL_BAD_SURFACE: return "EGL_BAD_SURFACE";
81 case EGL_CONTEXT_LOST: return "EGL_CONTEXT_LOST";
82 default: return "UNKNOWN";
83 }
84 }
85 };
86
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080087 EGLint error = eglGetError();
Mathias Agopiancbb288b2009-09-07 16:32:45 -070088 if (error && error != EGL_SUCCESS) {
Steve Blocke6f43dd2012-01-06 19:20:56 +000089 ALOGE("%s: EGL error 0x%04x (%s)",
Mathias Agopian0928e312009-08-07 16:38:10 -070090 token, int(error), EGLUtils::strerror(error));
Mathias Agopiancbb288b2009-09-07 16:32:45 -070091 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080092}
93
Mathias Agopiana4912602012-07-12 14:25:33 -070094// ----------------------------------------------------------------------------
95
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080096/*
97 * Initialize the display to the specified values.
98 *
99 */
100
101DisplayHardware::DisplayHardware(
102 const sp<SurfaceFlinger>& flinger,
Mathias Agopiana4912602012-07-12 14:25:33 -0700103 int display,
104 const sp<SurfaceTextureClient>& surface,
105 EGLConfig config)
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700106 : DisplayHardwareBase(display),
Mathias Agopiana4912602012-07-12 14:25:33 -0700107 mFlinger(flinger),
108 mDisplayId(display),
Mathias Agopiana4912602012-07-12 14:25:33 -0700109 mNativeWindow(surface),
110 mFlags(0),
111 mSecureLayerVisible(false)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800112{
Mathias Agopiana4912602012-07-12 14:25:33 -0700113 init(config);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800114}
115
Mathias Agopiana4912602012-07-12 14:25:33 -0700116DisplayHardware::~DisplayHardware() {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800117}
118
Mathias Agopiana4912602012-07-12 14:25:33 -0700119float DisplayHardware::getDpiX() const {
120 return mDpiX;
Mathias Agopian3d64e732011-04-18 15:59:24 -0700121}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800122
Mathias Agopiana4912602012-07-12 14:25:33 -0700123float DisplayHardware::getDpiY() const {
124 return mDpiY;
Mathias Agopian61630912011-07-06 16:35:30 -0700125}
126
Mathias Agopiana4912602012-07-12 14:25:33 -0700127float DisplayHardware::getDensity() const {
128 return mDensity;
129}
Mathias Agopian61630912011-07-06 16:35:30 -0700130
Mathias Agopiana4912602012-07-12 14:25:33 -0700131float DisplayHardware::getRefreshRate() const {
132 return mRefreshRate;
133}
134
135int DisplayHardware::getWidth() const {
136 return mDisplayWidth;
137}
138
139int DisplayHardware::getHeight() const {
140 return mDisplayHeight;
141}
142
143PixelFormat DisplayHardware::getFormat() const {
144 return mFormat;
145}
146
147EGLSurface DisplayHardware::getEGLSurface() const {
148 return mSurface;
149}
150
Mathias Agopianc666cae2012-07-25 18:56:13 -0700151status_t DisplayHardware::getInfo(DisplayInfo* info) const {
152 info->w = getWidth();
153 info->h = getHeight();
154 info->xdpi = getDpiX();
155 info->ydpi = getDpiY();
156 info->fps = getRefreshRate();
157 info->density = getDensity();
158 info->orientation = getOrientation();
159 // TODO: this needs to go away (currently needed only by webkit)
160 getPixelFormatInfo(getFormat(), &info->pixelFormatInfo);
161 return NO_ERROR;
162}
163
Mathias Agopiana4912602012-07-12 14:25:33 -0700164void DisplayHardware::init(EGLConfig config)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800165{
Mathias Agopiana4912602012-07-12 14:25:33 -0700166 ANativeWindow* const window = mNativeWindow.get();
167
168 int concreteType;
169 window->query(window, NATIVE_WINDOW_CONCRETE_TYPE, &concreteType);
170 if (concreteType == NATIVE_WINDOW_FRAMEBUFFER) {
171 mFramebufferSurface = static_cast<FramebufferSurface *>(mNativeWindow.get());
Mathias Agopian1f339ff2011-07-01 17:08:43 -0700172 }
173
Mathias Agopian61630912011-07-06 16:35:30 -0700174 int format;
Mathias Agopian61630912011-07-06 16:35:30 -0700175 window->query(window, NATIVE_WINDOW_FORMAT, &format);
Mathias Agopiana4912602012-07-12 14:25:33 -0700176 mDpiX = window->xdpi;
177 mDpiY = window->ydpi;
178 if (mFramebufferSurface != NULL) {
179 mRefreshRate = mFramebufferSurface->getRefreshRate();
180 } else {
181 mRefreshRate = 60;
Mathias Agopianb5dd9c02012-03-22 12:15:54 -0700182 }
Mathias Agopiana4912602012-07-12 14:25:33 -0700183 mRefreshPeriod = nsecs_t(1e9 / mRefreshRate);
Mathias Agopian385977f2011-11-04 18:46:11 -0700184
Mathias Agopiana4912602012-07-12 14:25:33 -0700185
186 // TODO: Not sure if display density should handled by SF any longer
Mathias Agopianb5dd9c02012-03-22 12:15:54 -0700187 class Density {
188 static int getDensityFromProperty(char const* propName) {
189 char property[PROPERTY_VALUE_MAX];
190 int density = 0;
191 if (property_get(propName, property, NULL) > 0) {
192 density = atoi(property);
193 }
194 return density;
195 }
196 public:
197 static int getEmuDensity() {
198 return getDensityFromProperty("qemu.sf.lcd_density"); }
199 static int getBuildDensity() {
200 return getDensityFromProperty("ro.sf.lcd_density"); }
201 };
Mathias Agopianb5dd9c02012-03-22 12:15:54 -0700202 // The density of the device is provided by a build property
203 mDensity = Density::getBuildDensity() / 160.0f;
Mathias Agopianb5dd9c02012-03-22 12:15:54 -0700204 if (mDensity == 0) {
205 // the build doesn't provide a density -- this is wrong!
206 // use xdpi instead
207 ALOGE("ro.sf.lcd_density must be defined as a build property");
208 mDensity = mDpiX / 160.0f;
209 }
Mathias Agopianb5dd9c02012-03-22 12:15:54 -0700210 if (Density::getEmuDensity()) {
211 // if "qemu.sf.lcd_density" is specified, it overrides everything
212 mDpiX = mDpiY = mDensity = Density::getEmuDensity();
213 mDensity /= 160.0f;
214 }
215
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800216 /*
Mathias Agopiana4912602012-07-12 14:25:33 -0700217 * Create our display's surface
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800218 */
219
Mathias Agopiana4912602012-07-12 14:25:33 -0700220 EGLSurface surface;
221 EGLint w, h;
222 EGLDisplay display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
223 surface = eglCreateWindowSurface(display, config, window, NULL);
Mathias Agopian1b031492012-06-20 17:51:20 -0700224 eglQuerySurface(display, surface, EGL_WIDTH, &mDisplayWidth);
225 eglQuerySurface(display, surface, EGL_HEIGHT, &mDisplayHeight);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800226
Mathias Agopiana4912602012-07-12 14:25:33 -0700227 if (mFramebufferSurface != NULL) {
228 if (mFramebufferSurface->isUpdateOnDemand()) {
229 mFlags |= PARTIAL_UPDATES;
230 // if we have partial updates, we definitely don't need to
231 // preserve the backbuffer, which may be costly.
232 eglSurfaceAttrib(display, surface,
233 EGL_SWAP_BEHAVIOR, EGL_BUFFER_DESTROYED);
234 }
Mathias Agopian0928bee2009-09-16 20:15:42 -0700235 }
236
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800237 mDisplay = display;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800238 mSurface = surface;
Mathias Agopiana4912602012-07-12 14:25:33 -0700239 mFormat = format;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700240 mPageFlipCount = 0;
Mathias Agopian1f7bec62010-06-25 18:02:21 -0700241
Mathias Agopian98a121a2012-07-24 21:08:59 -0700242 // initialize the display orientation transform.
243 DisplayHardware::setOrientation(ISurfaceComposer::eOrientationDefault);
Mathias Agopiana350ff92010-08-10 17:14:02 -0700244}
245
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800246uint32_t DisplayHardware::getPageFlipCount() const {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700247 return mPageFlipCount;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800248}
249
Mathias Agopian82d7ab62012-01-19 18:34:40 -0800250nsecs_t DisplayHardware::getRefreshTimestamp() const {
251 // this returns the last refresh timestamp.
252 // if the last one is not available, we estimate it based on
253 // the refresh period and whatever closest timestamp we have.
Mathias Agopian3eb38cb2012-04-03 22:09:52 -0700254 Mutex::Autolock _l(mLock);
255 nsecs_t now = systemTime(CLOCK_MONOTONIC);
Mathias Agopian82d7ab62012-01-19 18:34:40 -0800256 return now - ((now - mLastHwVSync) % mRefreshPeriod);
257}
258
259nsecs_t DisplayHardware::getRefreshPeriod() const {
260 return mRefreshPeriod;
261}
262
Mathias Agopian74faca22009-09-17 16:18:16 -0700263status_t DisplayHardware::compositionComplete() const {
Mathias Agopiana4912602012-07-12 14:25:33 -0700264 if (mFramebufferSurface == NULL) {
265 return NO_ERROR;
266 }
267 return mFramebufferSurface->compositionComplete();
Mathias Agopian74faca22009-09-17 16:18:16 -0700268}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800269
Mathias Agopian86303202012-07-24 22:46:10 -0700270void DisplayHardware::onVSyncReceived(nsecs_t timestamp) {
271 Mutex::Autolock _l(mLock);
272 mLastHwVSync = timestamp;
273}
274
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800275void DisplayHardware::flip(const Region& dirty) const
276{
277 checkGLErrors();
278
279 EGLDisplay dpy = mDisplay;
280 EGLSurface surface = mSurface;
281
Mathias Agopian5e78e092009-06-11 17:19:54 -0700282#ifdef EGL_ANDROID_swap_rectangle
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700283 if (mFlags & SWAP_RECTANGLE) {
Mathias Agopianb8a55602009-06-26 19:06:36 -0700284 const Region newDirty(dirty.intersect(bounds()));
285 const Rect b(newDirty.getBounds());
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700286 eglSetSwapRectangleANDROID(dpy, surface,
287 b.left, b.top, b.width(), b.height());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800288 }
Mathias Agopian5e78e092009-06-11 17:19:54 -0700289#endif
290
Mathias Agopian95a666b2009-09-24 14:57:26 -0700291 if (mFlags & PARTIAL_UPDATES) {
Mathias Agopiana4912602012-07-12 14:25:33 -0700292 if (mFramebufferSurface != NULL) {
293 mFramebufferSurface->setUpdateRectangle(dirty.getBounds());
294 }
Mathias Agopian1e16b132009-05-07 17:40:23 -0700295 }
296
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700297 mPageFlipCount++;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800298}
299
300uint32_t DisplayHardware::getFlags() const
301{
302 return mFlags;
303}
304
Erik Gilling1d21a9c2010-12-01 16:38:01 -0800305void DisplayHardware::dump(String8& res) const
306{
Mathias Agopiana4912602012-07-12 14:25:33 -0700307 if (mFramebufferSurface != NULL) {
308 mFramebufferSurface->dump(res);
309 }
Erik Gilling1d21a9c2010-12-01 16:38:01 -0800310}
Mathias Agopian1b031492012-06-20 17:51:20 -0700311
312// ----------------------------------------------------------------------------
313
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700314void DisplayHardware::setVisibleLayersSortedByZ(const Vector< sp<LayerBase> >& layers) {
315 mVisibleLayersSortedByZ = layers;
316 size_t count = layers.size();
317 for (size_t i=0 ; i<count ; i++) {
318 if (layers[i]->isSecure()) {
319 mSecureLayerVisible = true;
320 }
321 }
322}
323
324Vector< sp<LayerBase> > DisplayHardware::getVisibleLayersSortedByZ() const {
325 return mVisibleLayersSortedByZ;
326}
327
328bool DisplayHardware::getSecureLayerVisible() const {
329 return mSecureLayerVisible;
330}
331
332// ----------------------------------------------------------------------------
333
Mathias Agopian1b031492012-06-20 17:51:20 -0700334status_t DisplayHardware::orientationToTransfrom(
335 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 Agopian98a121a2012-07-24 21:08:59 -0700358status_t DisplayHardware::setOrientation(int orientation) {
359 int w = mDisplayWidth;
360 int h = mDisplayHeight;
Mathias Agopian1b031492012-06-20 17:51:20 -0700361
Mathias Agopian98a121a2012-07-24 21:08:59 -0700362 DisplayHardware::orientationToTransfrom(
363 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 Agopian1b031492012-06-20 17:51:20 -0700370 return NO_ERROR;
371}