blob: af33a8959f97fef08244f048ddde1cbc22c86aff [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 Agopian076b1cc2009-04-10 14:24:30 -070027#include <ui/PixelFormat.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080028
29#include <GLES/gl.h>
Mathias Agopian076b1cc2009-04-10 14:24:30 -070030#include <EGL/egl.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080031#include <EGL/eglext.h>
32
Mathias Agopian076b1cc2009-04-10 14:24:30 -070033#include <hardware/gralloc.h>
Mathias Agopiana4912602012-07-12 14:25:33 -070034#include <private/gui/SharedBufferStack.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 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
100DisplayHardware::DisplayHardware(
101 const sp<SurfaceFlinger>& flinger,
Mathias Agopiana4912602012-07-12 14:25:33 -0700102 int display,
103 const sp<SurfaceTextureClient>& surface,
104 EGLConfig config)
105 : DisplayHardwareBase(flinger, display),
106 mFlinger(flinger),
107 mDisplayId(display),
108 mHwc(0),
109 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
151void DisplayHardware::init(EGLConfig config)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800152{
Mathias Agopiana4912602012-07-12 14:25:33 -0700153 ANativeWindow* const window = mNativeWindow.get();
154
155 int concreteType;
156 window->query(window, NATIVE_WINDOW_CONCRETE_TYPE, &concreteType);
157 if (concreteType == NATIVE_WINDOW_FRAMEBUFFER) {
158 mFramebufferSurface = static_cast<FramebufferSurface *>(mNativeWindow.get());
Mathias Agopian1f339ff2011-07-01 17:08:43 -0700159 }
160
Mathias Agopian61630912011-07-06 16:35:30 -0700161 int format;
Mathias Agopian61630912011-07-06 16:35:30 -0700162 window->query(window, NATIVE_WINDOW_FORMAT, &format);
Mathias Agopiana4912602012-07-12 14:25:33 -0700163 mDpiX = window->xdpi;
164 mDpiY = window->ydpi;
165 if (mFramebufferSurface != NULL) {
166 mRefreshRate = mFramebufferSurface->getRefreshRate();
167 } else {
168 mRefreshRate = 60;
Mathias Agopianb5dd9c02012-03-22 12:15:54 -0700169 }
Mathias Agopiana4912602012-07-12 14:25:33 -0700170 mRefreshPeriod = nsecs_t(1e9 / mRefreshRate);
Mathias Agopian385977f2011-11-04 18:46:11 -0700171
Mathias Agopiana4912602012-07-12 14:25:33 -0700172
173 // TODO: Not sure if display density should handled by SF any longer
Mathias Agopianb5dd9c02012-03-22 12:15:54 -0700174 class Density {
175 static int getDensityFromProperty(char const* propName) {
176 char property[PROPERTY_VALUE_MAX];
177 int density = 0;
178 if (property_get(propName, property, NULL) > 0) {
179 density = atoi(property);
180 }
181 return density;
182 }
183 public:
184 static int getEmuDensity() {
185 return getDensityFromProperty("qemu.sf.lcd_density"); }
186 static int getBuildDensity() {
187 return getDensityFromProperty("ro.sf.lcd_density"); }
188 };
Mathias Agopianb5dd9c02012-03-22 12:15:54 -0700189 // The density of the device is provided by a build property
190 mDensity = Density::getBuildDensity() / 160.0f;
Mathias Agopianb5dd9c02012-03-22 12:15:54 -0700191 if (mDensity == 0) {
192 // the build doesn't provide a density -- this is wrong!
193 // use xdpi instead
194 ALOGE("ro.sf.lcd_density must be defined as a build property");
195 mDensity = mDpiX / 160.0f;
196 }
Mathias Agopianb5dd9c02012-03-22 12:15:54 -0700197 if (Density::getEmuDensity()) {
198 // if "qemu.sf.lcd_density" is specified, it overrides everything
199 mDpiX = mDpiY = mDensity = Density::getEmuDensity();
200 mDensity /= 160.0f;
201 }
202
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800203 /*
Mathias Agopiana4912602012-07-12 14:25:33 -0700204 * Create our display's surface
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800205 */
206
Mathias Agopiana4912602012-07-12 14:25:33 -0700207 EGLSurface surface;
208 EGLint w, h;
209 EGLDisplay display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
210 surface = eglCreateWindowSurface(display, config, window, NULL);
Mathias Agopian1b031492012-06-20 17:51:20 -0700211 eglQuerySurface(display, surface, EGL_WIDTH, &mDisplayWidth);
212 eglQuerySurface(display, surface, EGL_HEIGHT, &mDisplayHeight);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800213
Mathias Agopiana4912602012-07-12 14:25:33 -0700214 if (mFramebufferSurface != NULL) {
215 if (mFramebufferSurface->isUpdateOnDemand()) {
216 mFlags |= PARTIAL_UPDATES;
217 // if we have partial updates, we definitely don't need to
218 // preserve the backbuffer, which may be costly.
219 eglSurfaceAttrib(display, surface,
220 EGL_SWAP_BEHAVIOR, EGL_BUFFER_DESTROYED);
221 }
Mathias Agopian0928bee2009-09-16 20:15:42 -0700222 }
223
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800224 mDisplay = display;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800225 mSurface = surface;
Mathias Agopiana4912602012-07-12 14:25:33 -0700226 mFormat = format;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700227 mPageFlipCount = 0;
Mathias Agopian1f7bec62010-06-25 18:02:21 -0700228
Mathias Agopiana350ff92010-08-10 17:14:02 -0700229 // initialize the H/W composer
Mathias Agopian3eb38cb2012-04-03 22:09:52 -0700230 mHwc = new HWComposer(mFlinger, *this, mRefreshPeriod);
Mathias Agopiana350ff92010-08-10 17:14:02 -0700231 if (mHwc->initCheck() == NO_ERROR) {
232 mHwc->setFrameBuffer(mDisplay, mSurface);
233 }
Mathias Agopian1b031492012-06-20 17:51:20 -0700234
Mathias Agopian1b031492012-06-20 17:51:20 -0700235 // initialize the display orientation transform.
236 // it's a constant that should come from the display driver.
237 int displayOrientation = ISurfaceComposer::eOrientationDefault;
238 char property[PROPERTY_VALUE_MAX];
239 if (property_get("ro.sf.hwrotation", property, NULL) > 0) {
240 //displayOrientation
241 switch (atoi(property)) {
242 case 90:
243 displayOrientation = ISurfaceComposer::eOrientation90;
244 break;
245 case 270:
246 displayOrientation = ISurfaceComposer::eOrientation270;
247 break;
248 }
249 }
250
251 w = mDisplayWidth;
252 h = mDisplayHeight;
253 DisplayHardware::orientationToTransfrom(displayOrientation, w, h,
254 &mDisplayTransform);
255 if (displayOrientation & ISurfaceComposer::eOrientationSwapMask) {
256 mLogicalDisplayWidth = h;
257 mLogicalDisplayHeight = w;
258 } else {
259 mLogicalDisplayWidth = w;
260 mLogicalDisplayHeight = h;
261 }
262 DisplayHardware::setOrientation(ISurfaceComposer::eOrientationDefault);
Mathias Agopiana4912602012-07-12 14:25:33 -0700263
264 // initialize the shared control block
265 surface_flinger_cblk_t* const scblk = mFlinger->getControlBlock();
266 scblk->connected |= 1 << mDisplayId;
267 display_cblk_t* dcblk = &scblk->displays[mDisplayId];
268 memset(dcblk, 0, sizeof(display_cblk_t));
269 dcblk->w = w; // XXX: plane.getWidth();
270 dcblk->h = h; // XXX: plane.getHeight();
271 dcblk->format = format;
272 dcblk->orientation = ISurfaceComposer::eOrientationDefault;
273 dcblk->xdpi = mDpiX;
274 dcblk->ydpi = mDpiY;
275 dcblk->fps = mRefreshRate;
276 dcblk->density = mDensity;
Mathias Agopiana350ff92010-08-10 17:14:02 -0700277}
278
Mathias Agopian3eb38cb2012-04-03 22:09:52 -0700279void DisplayHardware::setVSyncHandler(const sp<VSyncHandler>& handler) {
280 Mutex::Autolock _l(mLock);
281 mVSyncHandler = handler;
282}
283
Mathias Agopian03e40722012-04-26 16:11:59 -0700284void DisplayHardware::eventControl(int event, int enabled) {
285 if (event == EVENT_VSYNC) {
286 mPowerHAL.vsyncHint(enabled);
287 }
288 mHwc->eventControl(event, enabled);
289}
290
Mathias Agopian3eb38cb2012-04-03 22:09:52 -0700291void DisplayHardware::onVSyncReceived(int dpy, nsecs_t timestamp) {
292 sp<VSyncHandler> handler;
293 { // scope for the lock
294 Mutex::Autolock _l(mLock);
295 mLastHwVSync = timestamp;
296 if (mVSyncHandler != NULL) {
297 handler = mVSyncHandler.promote();
298 }
299 }
300
301 if (handler != NULL) {
302 handler->onVSyncReceived(dpy, timestamp);
303 }
304}
305
Mathias Agopiana350ff92010-08-10 17:14:02 -0700306HWComposer& DisplayHardware::getHwComposer() const {
307 return *mHwc;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800308}
309
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800310void DisplayHardware::releaseScreen() const
311{
312 DisplayHardwareBase::releaseScreen();
Antti Hatalaf5f27122010-09-09 02:33:05 -0700313 if (mHwc->initCheck() == NO_ERROR) {
314 mHwc->release();
315 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800316}
317
318void DisplayHardware::acquireScreen() const
319{
Colin Cross10fbdb62012-07-12 17:56:34 -0700320 if (mHwc->initCheck() == NO_ERROR) {
321 mHwc->acquire();
322 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800323 DisplayHardwareBase::acquireScreen();
324}
325
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800326uint32_t DisplayHardware::getPageFlipCount() const {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700327 return mPageFlipCount;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800328}
329
Mathias Agopian82d7ab62012-01-19 18:34:40 -0800330nsecs_t DisplayHardware::getRefreshTimestamp() const {
331 // this returns the last refresh timestamp.
332 // if the last one is not available, we estimate it based on
333 // the refresh period and whatever closest timestamp we have.
Mathias Agopian3eb38cb2012-04-03 22:09:52 -0700334 Mutex::Autolock _l(mLock);
335 nsecs_t now = systemTime(CLOCK_MONOTONIC);
Mathias Agopian82d7ab62012-01-19 18:34:40 -0800336 return now - ((now - mLastHwVSync) % mRefreshPeriod);
337}
338
339nsecs_t DisplayHardware::getRefreshPeriod() const {
340 return mRefreshPeriod;
341}
342
Mathias Agopian74faca22009-09-17 16:18:16 -0700343status_t DisplayHardware::compositionComplete() const {
Mathias Agopiana4912602012-07-12 14:25:33 -0700344 if (mFramebufferSurface == NULL) {
345 return NO_ERROR;
346 }
347 return mFramebufferSurface->compositionComplete();
Mathias Agopian74faca22009-09-17 16:18:16 -0700348}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800349
350void DisplayHardware::flip(const Region& dirty) const
351{
352 checkGLErrors();
353
354 EGLDisplay dpy = mDisplay;
355 EGLSurface surface = mSurface;
356
Mathias Agopian5e78e092009-06-11 17:19:54 -0700357#ifdef EGL_ANDROID_swap_rectangle
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700358 if (mFlags & SWAP_RECTANGLE) {
Mathias Agopianb8a55602009-06-26 19:06:36 -0700359 const Region newDirty(dirty.intersect(bounds()));
360 const Rect b(newDirty.getBounds());
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700361 eglSetSwapRectangleANDROID(dpy, surface,
362 b.left, b.top, b.width(), b.height());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800363 }
Mathias Agopian5e78e092009-06-11 17:19:54 -0700364#endif
365
Mathias Agopian95a666b2009-09-24 14:57:26 -0700366 if (mFlags & PARTIAL_UPDATES) {
Mathias Agopiana4912602012-07-12 14:25:33 -0700367 if (mFramebufferSurface != NULL) {
368 mFramebufferSurface->setUpdateRectangle(dirty.getBounds());
369 }
Mathias Agopian1e16b132009-05-07 17:40:23 -0700370 }
371
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700372 mPageFlipCount++;
Mathias Agopiana350ff92010-08-10 17:14:02 -0700373
374 if (mHwc->initCheck() == NO_ERROR) {
375 mHwc->commit();
376 } else {
377 eglSwapBuffers(dpy, surface);
378 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800379 checkEGLErrors("eglSwapBuffers");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800380}
381
382uint32_t DisplayHardware::getFlags() const
383{
384 return mFlags;
385}
386
Erik Gilling1d21a9c2010-12-01 16:38:01 -0800387void DisplayHardware::dump(String8& res) const
388{
Mathias Agopiana4912602012-07-12 14:25:33 -0700389 if (mFramebufferSurface != NULL) {
390 mFramebufferSurface->dump(res);
391 }
Erik Gilling1d21a9c2010-12-01 16:38:01 -0800392}
Mathias Agopian1b031492012-06-20 17:51:20 -0700393
394// ----------------------------------------------------------------------------
395
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700396void DisplayHardware::setVisibleLayersSortedByZ(const Vector< sp<LayerBase> >& layers) {
397 mVisibleLayersSortedByZ = layers;
398 size_t count = layers.size();
399 for (size_t i=0 ; i<count ; i++) {
400 if (layers[i]->isSecure()) {
401 mSecureLayerVisible = true;
402 }
403 }
404}
405
406Vector< sp<LayerBase> > DisplayHardware::getVisibleLayersSortedByZ() const {
407 return mVisibleLayersSortedByZ;
408}
409
410bool DisplayHardware::getSecureLayerVisible() const {
411 return mSecureLayerVisible;
412}
413
414// ----------------------------------------------------------------------------
415
Mathias Agopian1b031492012-06-20 17:51:20 -0700416status_t DisplayHardware::orientationToTransfrom(
417 int orientation, int w, int h, Transform* tr)
418{
419 uint32_t flags = 0;
420 switch (orientation) {
421 case ISurfaceComposer::eOrientationDefault:
422 flags = Transform::ROT_0;
423 break;
424 case ISurfaceComposer::eOrientation90:
425 flags = Transform::ROT_90;
426 break;
427 case ISurfaceComposer::eOrientation180:
428 flags = Transform::ROT_180;
429 break;
430 case ISurfaceComposer::eOrientation270:
431 flags = Transform::ROT_270;
432 break;
433 default:
434 return BAD_VALUE;
435 }
436 tr->set(flags, w, h);
437 return NO_ERROR;
438}
439
440status_t DisplayHardware::setOrientation(int orientation)
441{
442 // If the rotation can be handled in hardware, this is where
443 // the magic should happen.
444
445 const int w = mLogicalDisplayWidth;
446 const int h = mLogicalDisplayHeight;
447 mUserDisplayWidth = w;
448 mUserDisplayHeight = h;
449
450 Transform orientationTransform;
451 DisplayHardware::orientationToTransfrom(orientation, w, h,
452 &orientationTransform);
453 if (orientation & ISurfaceComposer::eOrientationSwapMask) {
454 mUserDisplayWidth = h;
455 mUserDisplayHeight = w;
456 }
457
458 mOrientation = orientation;
459 mGlobalTransform = mDisplayTransform * orientationTransform;
460 return NO_ERROR;
461}