blob: 6a8a55dc7e2366c5d51cd2da7ccc626e0f48ca08 [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 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),
109 mHwc(0),
110 mNativeWindow(surface),
111 mFlags(0),
112 mSecureLayerVisible(false)
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
152void DisplayHardware::init(EGLConfig config)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800153{
Mathias Agopiana4912602012-07-12 14:25:33 -0700154 ANativeWindow* const window = mNativeWindow.get();
155
156 int concreteType;
157 window->query(window, NATIVE_WINDOW_CONCRETE_TYPE, &concreteType);
158 if (concreteType == NATIVE_WINDOW_FRAMEBUFFER) {
159 mFramebufferSurface = static_cast<FramebufferSurface *>(mNativeWindow.get());
Mathias Agopian1f339ff2011-07-01 17:08:43 -0700160 }
161
Mathias Agopian61630912011-07-06 16:35:30 -0700162 int format;
Mathias Agopian61630912011-07-06 16:35:30 -0700163 window->query(window, NATIVE_WINDOW_FORMAT, &format);
Mathias Agopiana4912602012-07-12 14:25:33 -0700164 mDpiX = window->xdpi;
165 mDpiY = window->ydpi;
166 if (mFramebufferSurface != NULL) {
167 mRefreshRate = mFramebufferSurface->getRefreshRate();
168 } else {
169 mRefreshRate = 60;
Mathias Agopianb5dd9c02012-03-22 12:15:54 -0700170 }
Mathias Agopiana4912602012-07-12 14:25:33 -0700171 mRefreshPeriod = nsecs_t(1e9 / mRefreshRate);
Mathias Agopian385977f2011-11-04 18:46:11 -0700172
Mathias Agopiana4912602012-07-12 14:25:33 -0700173
174 // TODO: Not sure if display density should handled by SF any longer
Mathias Agopianb5dd9c02012-03-22 12:15:54 -0700175 class Density {
176 static int getDensityFromProperty(char const* propName) {
177 char property[PROPERTY_VALUE_MAX];
178 int density = 0;
179 if (property_get(propName, property, NULL) > 0) {
180 density = atoi(property);
181 }
182 return density;
183 }
184 public:
185 static int getEmuDensity() {
186 return getDensityFromProperty("qemu.sf.lcd_density"); }
187 static int getBuildDensity() {
188 return getDensityFromProperty("ro.sf.lcd_density"); }
189 };
Mathias Agopianb5dd9c02012-03-22 12:15:54 -0700190 // The density of the device is provided by a build property
191 mDensity = Density::getBuildDensity() / 160.0f;
Mathias Agopianb5dd9c02012-03-22 12:15:54 -0700192 if (mDensity == 0) {
193 // the build doesn't provide a density -- this is wrong!
194 // use xdpi instead
195 ALOGE("ro.sf.lcd_density must be defined as a build property");
196 mDensity = mDpiX / 160.0f;
197 }
Mathias Agopianb5dd9c02012-03-22 12:15:54 -0700198 if (Density::getEmuDensity()) {
199 // if "qemu.sf.lcd_density" is specified, it overrides everything
200 mDpiX = mDpiY = mDensity = Density::getEmuDensity();
201 mDensity /= 160.0f;
202 }
203
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800204 /*
Mathias Agopiana4912602012-07-12 14:25:33 -0700205 * Create our display's surface
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800206 */
207
Mathias Agopiana4912602012-07-12 14:25:33 -0700208 EGLSurface surface;
209 EGLint w, h;
210 EGLDisplay display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
211 surface = eglCreateWindowSurface(display, config, window, NULL);
Mathias Agopian1b031492012-06-20 17:51:20 -0700212 eglQuerySurface(display, surface, EGL_WIDTH, &mDisplayWidth);
213 eglQuerySurface(display, surface, EGL_HEIGHT, &mDisplayHeight);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800214
Mathias Agopiana4912602012-07-12 14:25:33 -0700215 if (mFramebufferSurface != NULL) {
216 if (mFramebufferSurface->isUpdateOnDemand()) {
217 mFlags |= PARTIAL_UPDATES;
218 // if we have partial updates, we definitely don't need to
219 // preserve the backbuffer, which may be costly.
220 eglSurfaceAttrib(display, surface,
221 EGL_SWAP_BEHAVIOR, EGL_BUFFER_DESTROYED);
222 }
Mathias Agopian0928bee2009-09-16 20:15:42 -0700223 }
224
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800225 mDisplay = display;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800226 mSurface = surface;
Mathias Agopiana4912602012-07-12 14:25:33 -0700227 mFormat = format;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700228 mPageFlipCount = 0;
Mathias Agopian1f7bec62010-06-25 18:02:21 -0700229
Mathias Agopiana350ff92010-08-10 17:14:02 -0700230 // initialize the H/W composer
Mathias Agopian3eb38cb2012-04-03 22:09:52 -0700231 mHwc = new HWComposer(mFlinger, *this, mRefreshPeriod);
Mathias Agopiana350ff92010-08-10 17:14:02 -0700232 if (mHwc->initCheck() == NO_ERROR) {
233 mHwc->setFrameBuffer(mDisplay, mSurface);
234 }
Mathias Agopian1b031492012-06-20 17:51:20 -0700235
Mathias Agopian1b031492012-06-20 17:51:20 -0700236 // initialize the display orientation transform.
237 // it's a constant that should come from the display driver.
238 int displayOrientation = ISurfaceComposer::eOrientationDefault;
239 char property[PROPERTY_VALUE_MAX];
240 if (property_get("ro.sf.hwrotation", property, NULL) > 0) {
241 //displayOrientation
242 switch (atoi(property)) {
243 case 90:
244 displayOrientation = ISurfaceComposer::eOrientation90;
245 break;
246 case 270:
247 displayOrientation = ISurfaceComposer::eOrientation270;
248 break;
249 }
250 }
251
252 w = mDisplayWidth;
253 h = mDisplayHeight;
254 DisplayHardware::orientationToTransfrom(displayOrientation, w, h,
255 &mDisplayTransform);
256 if (displayOrientation & ISurfaceComposer::eOrientationSwapMask) {
257 mLogicalDisplayWidth = h;
258 mLogicalDisplayHeight = w;
259 } else {
260 mLogicalDisplayWidth = w;
261 mLogicalDisplayHeight = h;
262 }
263 DisplayHardware::setOrientation(ISurfaceComposer::eOrientationDefault);
Mathias Agopiana4912602012-07-12 14:25:33 -0700264
265 // initialize the shared control block
266 surface_flinger_cblk_t* const scblk = mFlinger->getControlBlock();
267 scblk->connected |= 1 << mDisplayId;
268 display_cblk_t* dcblk = &scblk->displays[mDisplayId];
269 memset(dcblk, 0, sizeof(display_cblk_t));
270 dcblk->w = w; // XXX: plane.getWidth();
271 dcblk->h = h; // XXX: plane.getHeight();
272 dcblk->format = format;
273 dcblk->orientation = ISurfaceComposer::eOrientationDefault;
274 dcblk->xdpi = mDpiX;
275 dcblk->ydpi = mDpiY;
276 dcblk->fps = mRefreshRate;
277 dcblk->density = mDensity;
Mathias Agopiana350ff92010-08-10 17:14:02 -0700278}
279
Mathias Agopian3eb38cb2012-04-03 22:09:52 -0700280void DisplayHardware::setVSyncHandler(const sp<VSyncHandler>& handler) {
281 Mutex::Autolock _l(mLock);
282 mVSyncHandler = handler;
283}
284
Mathias Agopian03e40722012-04-26 16:11:59 -0700285void DisplayHardware::eventControl(int event, int enabled) {
286 if (event == EVENT_VSYNC) {
287 mPowerHAL.vsyncHint(enabled);
288 }
289 mHwc->eventControl(event, enabled);
290}
291
Mathias Agopian3eb38cb2012-04-03 22:09:52 -0700292void DisplayHardware::onVSyncReceived(int dpy, nsecs_t timestamp) {
293 sp<VSyncHandler> handler;
294 { // scope for the lock
295 Mutex::Autolock _l(mLock);
296 mLastHwVSync = timestamp;
297 if (mVSyncHandler != NULL) {
298 handler = mVSyncHandler.promote();
299 }
300 }
301
302 if (handler != NULL) {
303 handler->onVSyncReceived(dpy, timestamp);
304 }
305}
306
Mathias Agopiana350ff92010-08-10 17:14:02 -0700307HWComposer& DisplayHardware::getHwComposer() const {
308 return *mHwc;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800309}
310
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800311void DisplayHardware::releaseScreen() const
312{
313 DisplayHardwareBase::releaseScreen();
Antti Hatalaf5f27122010-09-09 02:33:05 -0700314 if (mHwc->initCheck() == NO_ERROR) {
315 mHwc->release();
316 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800317}
318
319void DisplayHardware::acquireScreen() const
320{
Colin Cross10fbdb62012-07-12 17:56:34 -0700321 if (mHwc->initCheck() == NO_ERROR) {
322 mHwc->acquire();
323 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800324 DisplayHardwareBase::acquireScreen();
325}
326
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800327uint32_t DisplayHardware::getPageFlipCount() const {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700328 return mPageFlipCount;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800329}
330
Mathias Agopian82d7ab62012-01-19 18:34:40 -0800331nsecs_t DisplayHardware::getRefreshTimestamp() const {
332 // this returns the last refresh timestamp.
333 // if the last one is not available, we estimate it based on
334 // the refresh period and whatever closest timestamp we have.
Mathias Agopian3eb38cb2012-04-03 22:09:52 -0700335 Mutex::Autolock _l(mLock);
336 nsecs_t now = systemTime(CLOCK_MONOTONIC);
Mathias Agopian82d7ab62012-01-19 18:34:40 -0800337 return now - ((now - mLastHwVSync) % mRefreshPeriod);
338}
339
340nsecs_t DisplayHardware::getRefreshPeriod() const {
341 return mRefreshPeriod;
342}
343
Mathias Agopian74faca22009-09-17 16:18:16 -0700344status_t DisplayHardware::compositionComplete() const {
Mathias Agopiana4912602012-07-12 14:25:33 -0700345 if (mFramebufferSurface == NULL) {
346 return NO_ERROR;
347 }
348 return mFramebufferSurface->compositionComplete();
Mathias Agopian74faca22009-09-17 16:18:16 -0700349}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800350
351void DisplayHardware::flip(const Region& dirty) const
352{
353 checkGLErrors();
354
355 EGLDisplay dpy = mDisplay;
356 EGLSurface surface = mSurface;
357
Mathias Agopian5e78e092009-06-11 17:19:54 -0700358#ifdef EGL_ANDROID_swap_rectangle
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700359 if (mFlags & SWAP_RECTANGLE) {
Mathias Agopianb8a55602009-06-26 19:06:36 -0700360 const Region newDirty(dirty.intersect(bounds()));
361 const Rect b(newDirty.getBounds());
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700362 eglSetSwapRectangleANDROID(dpy, surface,
363 b.left, b.top, b.width(), b.height());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800364 }
Mathias Agopian5e78e092009-06-11 17:19:54 -0700365#endif
366
Mathias Agopian95a666b2009-09-24 14:57:26 -0700367 if (mFlags & PARTIAL_UPDATES) {
Mathias Agopiana4912602012-07-12 14:25:33 -0700368 if (mFramebufferSurface != NULL) {
369 mFramebufferSurface->setUpdateRectangle(dirty.getBounds());
370 }
Mathias Agopian1e16b132009-05-07 17:40:23 -0700371 }
372
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700373 mPageFlipCount++;
Mathias Agopiana350ff92010-08-10 17:14:02 -0700374
375 if (mHwc->initCheck() == NO_ERROR) {
376 mHwc->commit();
377 } else {
378 eglSwapBuffers(dpy, surface);
379 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800380 checkEGLErrors("eglSwapBuffers");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800381}
382
383uint32_t DisplayHardware::getFlags() const
384{
385 return mFlags;
386}
387
Erik Gilling1d21a9c2010-12-01 16:38:01 -0800388void DisplayHardware::dump(String8& res) const
389{
Mathias Agopiana4912602012-07-12 14:25:33 -0700390 if (mFramebufferSurface != NULL) {
391 mFramebufferSurface->dump(res);
392 }
Erik Gilling1d21a9c2010-12-01 16:38:01 -0800393}
Mathias Agopian1b031492012-06-20 17:51:20 -0700394
395// ----------------------------------------------------------------------------
396
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700397void DisplayHardware::setVisibleLayersSortedByZ(const Vector< sp<LayerBase> >& layers) {
398 mVisibleLayersSortedByZ = layers;
399 size_t count = layers.size();
400 for (size_t i=0 ; i<count ; i++) {
401 if (layers[i]->isSecure()) {
402 mSecureLayerVisible = true;
403 }
404 }
405}
406
407Vector< sp<LayerBase> > DisplayHardware::getVisibleLayersSortedByZ() const {
408 return mVisibleLayersSortedByZ;
409}
410
411bool DisplayHardware::getSecureLayerVisible() const {
412 return mSecureLayerVisible;
413}
414
415// ----------------------------------------------------------------------------
416
Mathias Agopian1b031492012-06-20 17:51:20 -0700417status_t DisplayHardware::orientationToTransfrom(
418 int orientation, int w, int h, Transform* tr)
419{
420 uint32_t flags = 0;
421 switch (orientation) {
422 case ISurfaceComposer::eOrientationDefault:
423 flags = Transform::ROT_0;
424 break;
425 case ISurfaceComposer::eOrientation90:
426 flags = Transform::ROT_90;
427 break;
428 case ISurfaceComposer::eOrientation180:
429 flags = Transform::ROT_180;
430 break;
431 case ISurfaceComposer::eOrientation270:
432 flags = Transform::ROT_270;
433 break;
434 default:
435 return BAD_VALUE;
436 }
437 tr->set(flags, w, h);
438 return NO_ERROR;
439}
440
441status_t DisplayHardware::setOrientation(int orientation)
442{
443 // If the rotation can be handled in hardware, this is where
444 // the magic should happen.
445
446 const int w = mLogicalDisplayWidth;
447 const int h = mLogicalDisplayHeight;
448 mUserDisplayWidth = w;
449 mUserDisplayHeight = h;
450
451 Transform orientationTransform;
452 DisplayHardware::orientationToTransfrom(orientation, w, h,
453 &orientationTransform);
454 if (orientation & ISurfaceComposer::eOrientationSwapMask) {
455 mUserDisplayWidth = h;
456 mUserDisplayHeight = w;
457 }
458
459 mOrientation = orientation;
460 mGlobalTransform = mDisplayTransform * orientationTransform;
461 return NO_ERROR;
462}