blob: b468aaefc6877cf2e952364792be0aac4272bd55 [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),
Mathias Agopian87baae12012-07-31 12:38:26 -0700111 mSecureLayerVisible(false),
112 mLayerStack(0)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800113{
Mathias Agopiana4912602012-07-12 14:25:33 -0700114 init(config);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800115}
116
Mathias Agopiana4912602012-07-12 14:25:33 -0700117DisplayHardware::~DisplayHardware() {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800118}
119
Mathias Agopiana4912602012-07-12 14:25:33 -0700120float DisplayHardware::getDpiX() const {
121 return mDpiX;
Mathias Agopian3d64e732011-04-18 15:59:24 -0700122}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800123
Mathias Agopiana4912602012-07-12 14:25:33 -0700124float DisplayHardware::getDpiY() const {
125 return mDpiY;
Mathias Agopian61630912011-07-06 16:35:30 -0700126}
127
Mathias Agopiana4912602012-07-12 14:25:33 -0700128float DisplayHardware::getDensity() const {
129 return mDensity;
130}
Mathias Agopian61630912011-07-06 16:35:30 -0700131
Mathias Agopiana4912602012-07-12 14:25:33 -0700132float DisplayHardware::getRefreshRate() const {
133 return mRefreshRate;
134}
135
136int DisplayHardware::getWidth() const {
137 return mDisplayWidth;
138}
139
140int DisplayHardware::getHeight() const {
141 return mDisplayHeight;
142}
143
144PixelFormat DisplayHardware::getFormat() const {
145 return mFormat;
146}
147
148EGLSurface DisplayHardware::getEGLSurface() const {
149 return mSurface;
150}
151
Mathias Agopianc666cae2012-07-25 18:56:13 -0700152status_t DisplayHardware::getInfo(DisplayInfo* info) const {
153 info->w = getWidth();
154 info->h = getHeight();
155 info->xdpi = getDpiX();
156 info->ydpi = getDpiY();
157 info->fps = getRefreshRate();
158 info->density = getDensity();
159 info->orientation = getOrientation();
160 // TODO: this needs to go away (currently needed only by webkit)
161 getPixelFormatInfo(getFormat(), &info->pixelFormatInfo);
162 return NO_ERROR;
163}
164
Mathias Agopiana4912602012-07-12 14:25:33 -0700165void DisplayHardware::init(EGLConfig config)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800166{
Mathias Agopiana4912602012-07-12 14:25:33 -0700167 ANativeWindow* const window = mNativeWindow.get();
168
169 int concreteType;
170 window->query(window, NATIVE_WINDOW_CONCRETE_TYPE, &concreteType);
171 if (concreteType == NATIVE_WINDOW_FRAMEBUFFER) {
172 mFramebufferSurface = static_cast<FramebufferSurface *>(mNativeWindow.get());
Mathias Agopian1f339ff2011-07-01 17:08:43 -0700173 }
174
Mathias Agopian61630912011-07-06 16:35:30 -0700175 int format;
Mathias Agopian61630912011-07-06 16:35:30 -0700176 window->query(window, NATIVE_WINDOW_FORMAT, &format);
Mathias Agopiana4912602012-07-12 14:25:33 -0700177 mDpiX = window->xdpi;
178 mDpiY = window->ydpi;
179 if (mFramebufferSurface != NULL) {
180 mRefreshRate = mFramebufferSurface->getRefreshRate();
181 } else {
182 mRefreshRate = 60;
Mathias Agopianb5dd9c02012-03-22 12:15:54 -0700183 }
Mathias Agopiana4912602012-07-12 14:25:33 -0700184 mRefreshPeriod = nsecs_t(1e9 / mRefreshRate);
Mathias Agopian385977f2011-11-04 18:46:11 -0700185
Mathias Agopiana4912602012-07-12 14:25:33 -0700186
187 // TODO: Not sure if display density should handled by SF any longer
Mathias Agopianb5dd9c02012-03-22 12:15:54 -0700188 class Density {
189 static int getDensityFromProperty(char const* propName) {
190 char property[PROPERTY_VALUE_MAX];
191 int density = 0;
192 if (property_get(propName, property, NULL) > 0) {
193 density = atoi(property);
194 }
195 return density;
196 }
197 public:
198 static int getEmuDensity() {
199 return getDensityFromProperty("qemu.sf.lcd_density"); }
200 static int getBuildDensity() {
201 return getDensityFromProperty("ro.sf.lcd_density"); }
202 };
Mathias Agopianb5dd9c02012-03-22 12:15:54 -0700203 // The density of the device is provided by a build property
204 mDensity = Density::getBuildDensity() / 160.0f;
Mathias Agopianb5dd9c02012-03-22 12:15:54 -0700205 if (mDensity == 0) {
206 // the build doesn't provide a density -- this is wrong!
207 // use xdpi instead
208 ALOGE("ro.sf.lcd_density must be defined as a build property");
209 mDensity = mDpiX / 160.0f;
210 }
Mathias Agopianb5dd9c02012-03-22 12:15:54 -0700211 if (Density::getEmuDensity()) {
212 // if "qemu.sf.lcd_density" is specified, it overrides everything
213 mDpiX = mDpiY = mDensity = Density::getEmuDensity();
214 mDensity /= 160.0f;
215 }
216
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800217 /*
Mathias Agopiana4912602012-07-12 14:25:33 -0700218 * Create our display's surface
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800219 */
220
Mathias Agopiana4912602012-07-12 14:25:33 -0700221 EGLSurface surface;
222 EGLint w, h;
223 EGLDisplay display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
224 surface = eglCreateWindowSurface(display, config, window, NULL);
Mathias Agopian1b031492012-06-20 17:51:20 -0700225 eglQuerySurface(display, surface, EGL_WIDTH, &mDisplayWidth);
226 eglQuerySurface(display, surface, EGL_HEIGHT, &mDisplayHeight);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800227
Mathias Agopiana4912602012-07-12 14:25:33 -0700228 if (mFramebufferSurface != NULL) {
229 if (mFramebufferSurface->isUpdateOnDemand()) {
230 mFlags |= PARTIAL_UPDATES;
231 // if we have partial updates, we definitely don't need to
232 // preserve the backbuffer, which may be costly.
233 eglSurfaceAttrib(display, surface,
234 EGL_SWAP_BEHAVIOR, EGL_BUFFER_DESTROYED);
235 }
Mathias Agopian0928bee2009-09-16 20:15:42 -0700236 }
237
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800238 mDisplay = display;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800239 mSurface = surface;
Mathias Agopiana4912602012-07-12 14:25:33 -0700240 mFormat = format;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700241 mPageFlipCount = 0;
Mathias Agopian1f7bec62010-06-25 18:02:21 -0700242
Mathias Agopian98a121a2012-07-24 21:08:59 -0700243 // initialize the display orientation transform.
244 DisplayHardware::setOrientation(ISurfaceComposer::eOrientationDefault);
Mathias Agopiana350ff92010-08-10 17:14:02 -0700245}
246
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800247uint32_t DisplayHardware::getPageFlipCount() const {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700248 return mPageFlipCount;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800249}
250
Mathias Agopian82d7ab62012-01-19 18:34:40 -0800251nsecs_t DisplayHardware::getRefreshTimestamp() const {
252 // this returns the last refresh timestamp.
253 // if the last one is not available, we estimate it based on
254 // the refresh period and whatever closest timestamp we have.
Mathias Agopian3eb38cb2012-04-03 22:09:52 -0700255 Mutex::Autolock _l(mLock);
256 nsecs_t now = systemTime(CLOCK_MONOTONIC);
Mathias Agopian82d7ab62012-01-19 18:34:40 -0800257 return now - ((now - mLastHwVSync) % mRefreshPeriod);
258}
259
260nsecs_t DisplayHardware::getRefreshPeriod() const {
261 return mRefreshPeriod;
262}
263
Mathias Agopian74faca22009-09-17 16:18:16 -0700264status_t DisplayHardware::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 Agopian86303202012-07-24 22:46:10 -0700271void DisplayHardware::onVSyncReceived(nsecs_t timestamp) {
272 Mutex::Autolock _l(mLock);
273 mLastHwVSync = timestamp;
274}
275
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800276void DisplayHardware::flip(const Region& dirty) const
277{
278 checkGLErrors();
279
280 EGLDisplay dpy = mDisplay;
281 EGLSurface surface = mSurface;
282
Mathias Agopian5e78e092009-06-11 17:19:54 -0700283#ifdef EGL_ANDROID_swap_rectangle
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700284 if (mFlags & SWAP_RECTANGLE) {
Mathias Agopianb8a55602009-06-26 19:06:36 -0700285 const Region newDirty(dirty.intersect(bounds()));
286 const Rect b(newDirty.getBounds());
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700287 eglSetSwapRectangleANDROID(dpy, surface,
288 b.left, b.top, b.width(), b.height());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800289 }
Mathias Agopian5e78e092009-06-11 17:19:54 -0700290#endif
291
Mathias Agopian95a666b2009-09-24 14:57:26 -0700292 if (mFlags & PARTIAL_UPDATES) {
Mathias Agopiana4912602012-07-12 14:25:33 -0700293 if (mFramebufferSurface != NULL) {
294 mFramebufferSurface->setUpdateRectangle(dirty.getBounds());
295 }
Mathias Agopian1e16b132009-05-07 17:40:23 -0700296 }
297
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700298 mPageFlipCount++;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800299}
300
301uint32_t DisplayHardware::getFlags() const
302{
303 return mFlags;
304}
305
Erik Gilling1d21a9c2010-12-01 16:38:01 -0800306void DisplayHardware::dump(String8& res) const
307{
Mathias Agopiana4912602012-07-12 14:25:33 -0700308 if (mFramebufferSurface != NULL) {
309 mFramebufferSurface->dump(res);
310 }
Erik Gilling1d21a9c2010-12-01 16:38:01 -0800311}
Mathias Agopian1b031492012-06-20 17:51:20 -0700312
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700313void DisplayHardware::makeCurrent(const DisplayHardware& hw, EGLContext ctx) {
314 EGLSurface sur = eglGetCurrentSurface(EGL_DRAW);
315 if (sur != hw.mSurface) {
316 EGLDisplay dpy = eglGetCurrentDisplay();
317 eglMakeCurrent(dpy, hw.mSurface, hw.mSurface, ctx);
318 }
319}
320
Mathias Agopian1b031492012-06-20 17:51:20 -0700321// ----------------------------------------------------------------------------
322
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700323void DisplayHardware::setVisibleLayersSortedByZ(const Vector< sp<LayerBase> >& layers) {
324 mVisibleLayersSortedByZ = layers;
325 size_t count = layers.size();
326 for (size_t i=0 ; i<count ; i++) {
327 if (layers[i]->isSecure()) {
328 mSecureLayerVisible = true;
329 }
330 }
331}
332
333Vector< sp<LayerBase> > DisplayHardware::getVisibleLayersSortedByZ() const {
334 return mVisibleLayersSortedByZ;
335}
336
337bool DisplayHardware::getSecureLayerVisible() const {
338 return mSecureLayerVisible;
339}
340
341// ----------------------------------------------------------------------------
342
Mathias Agopian1b031492012-06-20 17:51:20 -0700343status_t DisplayHardware::orientationToTransfrom(
344 int orientation, int w, int h, Transform* tr)
345{
346 uint32_t flags = 0;
347 switch (orientation) {
348 case ISurfaceComposer::eOrientationDefault:
349 flags = Transform::ROT_0;
350 break;
351 case ISurfaceComposer::eOrientation90:
352 flags = Transform::ROT_90;
353 break;
354 case ISurfaceComposer::eOrientation180:
355 flags = Transform::ROT_180;
356 break;
357 case ISurfaceComposer::eOrientation270:
358 flags = Transform::ROT_270;
359 break;
360 default:
361 return BAD_VALUE;
362 }
363 tr->set(flags, w, h);
364 return NO_ERROR;
365}
366
Mathias Agopian98a121a2012-07-24 21:08:59 -0700367status_t DisplayHardware::setOrientation(int orientation) {
368 int w = mDisplayWidth;
369 int h = mDisplayHeight;
Mathias Agopian1b031492012-06-20 17:51:20 -0700370
Mathias Agopian98a121a2012-07-24 21:08:59 -0700371 DisplayHardware::orientationToTransfrom(
372 orientation, w, h, &mGlobalTransform);
Mathias Agopian1b031492012-06-20 17:51:20 -0700373 if (orientation & ISurfaceComposer::eOrientationSwapMask) {
Mathias Agopian98a121a2012-07-24 21:08:59 -0700374 int tmp = w;
375 w = h;
376 h = tmp;
Mathias Agopian1b031492012-06-20 17:51:20 -0700377 }
Mathias Agopian1b031492012-06-20 17:51:20 -0700378 mOrientation = orientation;
Mathias Agopian1b031492012-06-20 17:51:20 -0700379 return NO_ERROR;
380}