blob: f982794f3218610faa0f5665e1100ba97a7aeaab [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 Agopiana4912602012-07-12 14:25:33 -0700236 // initialize the shared control block
237 surface_flinger_cblk_t* const scblk = mFlinger->getControlBlock();
238 scblk->connected |= 1 << mDisplayId;
239 display_cblk_t* dcblk = &scblk->displays[mDisplayId];
240 memset(dcblk, 0, sizeof(display_cblk_t));
Mathias Agopiana4912602012-07-12 14:25:33 -0700241 dcblk->format = format;
Mathias Agopiana4912602012-07-12 14:25:33 -0700242 dcblk->xdpi = mDpiX;
243 dcblk->ydpi = mDpiY;
244 dcblk->fps = mRefreshRate;
245 dcblk->density = mDensity;
Mathias Agopian98a121a2012-07-24 21:08:59 -0700246
247 // initialize the display orientation transform.
248 DisplayHardware::setOrientation(ISurfaceComposer::eOrientationDefault);
Mathias Agopiana350ff92010-08-10 17:14:02 -0700249}
250
Mathias Agopian3eb38cb2012-04-03 22:09:52 -0700251void DisplayHardware::setVSyncHandler(const sp<VSyncHandler>& handler) {
252 Mutex::Autolock _l(mLock);
253 mVSyncHandler = handler;
254}
255
Mathias Agopian03e40722012-04-26 16:11:59 -0700256void DisplayHardware::eventControl(int event, int enabled) {
257 if (event == EVENT_VSYNC) {
258 mPowerHAL.vsyncHint(enabled);
259 }
260 mHwc->eventControl(event, enabled);
261}
262
Mathias Agopian3eb38cb2012-04-03 22:09:52 -0700263void DisplayHardware::onVSyncReceived(int dpy, nsecs_t timestamp) {
264 sp<VSyncHandler> handler;
265 { // scope for the lock
266 Mutex::Autolock _l(mLock);
267 mLastHwVSync = timestamp;
268 if (mVSyncHandler != NULL) {
269 handler = mVSyncHandler.promote();
270 }
271 }
272
273 if (handler != NULL) {
274 handler->onVSyncReceived(dpy, timestamp);
275 }
276}
277
Mathias Agopiana350ff92010-08-10 17:14:02 -0700278HWComposer& DisplayHardware::getHwComposer() const {
279 return *mHwc;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800280}
281
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800282void DisplayHardware::releaseScreen() const
283{
284 DisplayHardwareBase::releaseScreen();
Antti Hatalaf5f27122010-09-09 02:33:05 -0700285 if (mHwc->initCheck() == NO_ERROR) {
286 mHwc->release();
287 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800288}
289
290void DisplayHardware::acquireScreen() const
291{
Colin Cross10fbdb62012-07-12 17:56:34 -0700292 if (mHwc->initCheck() == NO_ERROR) {
293 mHwc->acquire();
294 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800295 DisplayHardwareBase::acquireScreen();
296}
297
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800298uint32_t DisplayHardware::getPageFlipCount() const {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700299 return mPageFlipCount;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800300}
301
Mathias Agopian82d7ab62012-01-19 18:34:40 -0800302nsecs_t DisplayHardware::getRefreshTimestamp() const {
303 // this returns the last refresh timestamp.
304 // if the last one is not available, we estimate it based on
305 // the refresh period and whatever closest timestamp we have.
Mathias Agopian3eb38cb2012-04-03 22:09:52 -0700306 Mutex::Autolock _l(mLock);
307 nsecs_t now = systemTime(CLOCK_MONOTONIC);
Mathias Agopian82d7ab62012-01-19 18:34:40 -0800308 return now - ((now - mLastHwVSync) % mRefreshPeriod);
309}
310
311nsecs_t DisplayHardware::getRefreshPeriod() const {
312 return mRefreshPeriod;
313}
314
Mathias Agopian74faca22009-09-17 16:18:16 -0700315status_t DisplayHardware::compositionComplete() const {
Mathias Agopiana4912602012-07-12 14:25:33 -0700316 if (mFramebufferSurface == NULL) {
317 return NO_ERROR;
318 }
319 return mFramebufferSurface->compositionComplete();
Mathias Agopian74faca22009-09-17 16:18:16 -0700320}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800321
322void DisplayHardware::flip(const Region& dirty) const
323{
324 checkGLErrors();
325
326 EGLDisplay dpy = mDisplay;
327 EGLSurface surface = mSurface;
328
Mathias Agopian5e78e092009-06-11 17:19:54 -0700329#ifdef EGL_ANDROID_swap_rectangle
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700330 if (mFlags & SWAP_RECTANGLE) {
Mathias Agopianb8a55602009-06-26 19:06:36 -0700331 const Region newDirty(dirty.intersect(bounds()));
332 const Rect b(newDirty.getBounds());
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700333 eglSetSwapRectangleANDROID(dpy, surface,
334 b.left, b.top, b.width(), b.height());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800335 }
Mathias Agopian5e78e092009-06-11 17:19:54 -0700336#endif
337
Mathias Agopian95a666b2009-09-24 14:57:26 -0700338 if (mFlags & PARTIAL_UPDATES) {
Mathias Agopiana4912602012-07-12 14:25:33 -0700339 if (mFramebufferSurface != NULL) {
340 mFramebufferSurface->setUpdateRectangle(dirty.getBounds());
341 }
Mathias Agopian1e16b132009-05-07 17:40:23 -0700342 }
343
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700344 mPageFlipCount++;
Mathias Agopiana350ff92010-08-10 17:14:02 -0700345
346 if (mHwc->initCheck() == NO_ERROR) {
347 mHwc->commit();
348 } else {
349 eglSwapBuffers(dpy, surface);
350 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800351 checkEGLErrors("eglSwapBuffers");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800352}
353
354uint32_t DisplayHardware::getFlags() const
355{
356 return mFlags;
357}
358
Erik Gilling1d21a9c2010-12-01 16:38:01 -0800359void DisplayHardware::dump(String8& res) const
360{
Mathias Agopiana4912602012-07-12 14:25:33 -0700361 if (mFramebufferSurface != NULL) {
362 mFramebufferSurface->dump(res);
363 }
Erik Gilling1d21a9c2010-12-01 16:38:01 -0800364}
Mathias Agopian1b031492012-06-20 17:51:20 -0700365
366// ----------------------------------------------------------------------------
367
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700368void DisplayHardware::setVisibleLayersSortedByZ(const Vector< sp<LayerBase> >& layers) {
369 mVisibleLayersSortedByZ = layers;
370 size_t count = layers.size();
371 for (size_t i=0 ; i<count ; i++) {
372 if (layers[i]->isSecure()) {
373 mSecureLayerVisible = true;
374 }
375 }
376}
377
378Vector< sp<LayerBase> > DisplayHardware::getVisibleLayersSortedByZ() const {
379 return mVisibleLayersSortedByZ;
380}
381
382bool DisplayHardware::getSecureLayerVisible() const {
383 return mSecureLayerVisible;
384}
385
386// ----------------------------------------------------------------------------
387
Mathias Agopian1b031492012-06-20 17:51:20 -0700388status_t DisplayHardware::orientationToTransfrom(
389 int orientation, int w, int h, Transform* tr)
390{
391 uint32_t flags = 0;
392 switch (orientation) {
393 case ISurfaceComposer::eOrientationDefault:
394 flags = Transform::ROT_0;
395 break;
396 case ISurfaceComposer::eOrientation90:
397 flags = Transform::ROT_90;
398 break;
399 case ISurfaceComposer::eOrientation180:
400 flags = Transform::ROT_180;
401 break;
402 case ISurfaceComposer::eOrientation270:
403 flags = Transform::ROT_270;
404 break;
405 default:
406 return BAD_VALUE;
407 }
408 tr->set(flags, w, h);
409 return NO_ERROR;
410}
411
Mathias Agopian98a121a2012-07-24 21:08:59 -0700412status_t DisplayHardware::setOrientation(int orientation) {
413 int w = mDisplayWidth;
414 int h = mDisplayHeight;
Mathias Agopian1b031492012-06-20 17:51:20 -0700415
Mathias Agopian98a121a2012-07-24 21:08:59 -0700416 DisplayHardware::orientationToTransfrom(
417 orientation, w, h, &mGlobalTransform);
Mathias Agopian1b031492012-06-20 17:51:20 -0700418 if (orientation & ISurfaceComposer::eOrientationSwapMask) {
Mathias Agopian98a121a2012-07-24 21:08:59 -0700419 int tmp = w;
420 w = h;
421 h = tmp;
Mathias Agopian1b031492012-06-20 17:51:20 -0700422 }
Mathias Agopian1b031492012-06-20 17:51:20 -0700423 mOrientation = orientation;
Mathias Agopian98a121a2012-07-24 21:08:59 -0700424
425 // update the shared control block
426 surface_flinger_cblk_t* const scblk = mFlinger->getControlBlock();
427 volatile display_cblk_t* dcblk = &scblk->displays[mDisplayId];
428 dcblk->orientation = orientation;
429 dcblk->w = w;
430 dcblk->h = h;
431
Mathias Agopian1b031492012-06-20 17:51:20 -0700432 return NO_ERROR;
433}