blob: 067a54f6dbba05ed16ce3def5b43a44f06032545 [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),
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
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 Agopiana4912602012-07-12 14:25:33 -0700229 // initialize the shared control block
230 surface_flinger_cblk_t* const scblk = mFlinger->getControlBlock();
231 scblk->connected |= 1 << mDisplayId;
232 display_cblk_t* dcblk = &scblk->displays[mDisplayId];
233 memset(dcblk, 0, sizeof(display_cblk_t));
Mathias Agopiana4912602012-07-12 14:25:33 -0700234 dcblk->format = format;
Mathias Agopiana4912602012-07-12 14:25:33 -0700235 dcblk->xdpi = mDpiX;
236 dcblk->ydpi = mDpiY;
237 dcblk->fps = mRefreshRate;
238 dcblk->density = mDensity;
Mathias Agopian98a121a2012-07-24 21:08:59 -0700239
240 // initialize the display orientation transform.
241 DisplayHardware::setOrientation(ISurfaceComposer::eOrientationDefault);
Mathias Agopiana350ff92010-08-10 17:14:02 -0700242}
243
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800244uint32_t DisplayHardware::getPageFlipCount() const {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700245 return mPageFlipCount;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800246}
247
Mathias Agopian82d7ab62012-01-19 18:34:40 -0800248nsecs_t DisplayHardware::getRefreshTimestamp() const {
249 // this returns the last refresh timestamp.
250 // if the last one is not available, we estimate it based on
251 // the refresh period and whatever closest timestamp we have.
Mathias Agopian3eb38cb2012-04-03 22:09:52 -0700252 Mutex::Autolock _l(mLock);
253 nsecs_t now = systemTime(CLOCK_MONOTONIC);
Mathias Agopian82d7ab62012-01-19 18:34:40 -0800254 return now - ((now - mLastHwVSync) % mRefreshPeriod);
255}
256
257nsecs_t DisplayHardware::getRefreshPeriod() const {
258 return mRefreshPeriod;
259}
260
Mathias Agopian74faca22009-09-17 16:18:16 -0700261status_t DisplayHardware::compositionComplete() const {
Mathias Agopiana4912602012-07-12 14:25:33 -0700262 if (mFramebufferSurface == NULL) {
263 return NO_ERROR;
264 }
265 return mFramebufferSurface->compositionComplete();
Mathias Agopian74faca22009-09-17 16:18:16 -0700266}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800267
Mathias Agopian86303202012-07-24 22:46:10 -0700268void DisplayHardware::onVSyncReceived(nsecs_t timestamp) {
269 Mutex::Autolock _l(mLock);
270 mLastHwVSync = timestamp;
271}
272
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800273void DisplayHardware::flip(const Region& dirty) const
274{
275 checkGLErrors();
276
277 EGLDisplay dpy = mDisplay;
278 EGLSurface surface = mSurface;
279
Mathias Agopian5e78e092009-06-11 17:19:54 -0700280#ifdef EGL_ANDROID_swap_rectangle
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700281 if (mFlags & SWAP_RECTANGLE) {
Mathias Agopianb8a55602009-06-26 19:06:36 -0700282 const Region newDirty(dirty.intersect(bounds()));
283 const Rect b(newDirty.getBounds());
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700284 eglSetSwapRectangleANDROID(dpy, surface,
285 b.left, b.top, b.width(), b.height());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800286 }
Mathias Agopian5e78e092009-06-11 17:19:54 -0700287#endif
288
Mathias Agopian95a666b2009-09-24 14:57:26 -0700289 if (mFlags & PARTIAL_UPDATES) {
Mathias Agopiana4912602012-07-12 14:25:33 -0700290 if (mFramebufferSurface != NULL) {
291 mFramebufferSurface->setUpdateRectangle(dirty.getBounds());
292 }
Mathias Agopian1e16b132009-05-07 17:40:23 -0700293 }
294
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700295 mPageFlipCount++;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800296}
297
298uint32_t DisplayHardware::getFlags() const
299{
300 return mFlags;
301}
302
Erik Gilling1d21a9c2010-12-01 16:38:01 -0800303void DisplayHardware::dump(String8& res) const
304{
Mathias Agopiana4912602012-07-12 14:25:33 -0700305 if (mFramebufferSurface != NULL) {
306 mFramebufferSurface->dump(res);
307 }
Erik Gilling1d21a9c2010-12-01 16:38:01 -0800308}
Mathias Agopian1b031492012-06-20 17:51:20 -0700309
310// ----------------------------------------------------------------------------
311
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700312void DisplayHardware::setVisibleLayersSortedByZ(const Vector< sp<LayerBase> >& layers) {
313 mVisibleLayersSortedByZ = layers;
314 size_t count = layers.size();
315 for (size_t i=0 ; i<count ; i++) {
316 if (layers[i]->isSecure()) {
317 mSecureLayerVisible = true;
318 }
319 }
320}
321
322Vector< sp<LayerBase> > DisplayHardware::getVisibleLayersSortedByZ() const {
323 return mVisibleLayersSortedByZ;
324}
325
326bool DisplayHardware::getSecureLayerVisible() const {
327 return mSecureLayerVisible;
328}
329
330// ----------------------------------------------------------------------------
331
Mathias Agopian1b031492012-06-20 17:51:20 -0700332status_t DisplayHardware::orientationToTransfrom(
333 int orientation, int w, int h, Transform* tr)
334{
335 uint32_t flags = 0;
336 switch (orientation) {
337 case ISurfaceComposer::eOrientationDefault:
338 flags = Transform::ROT_0;
339 break;
340 case ISurfaceComposer::eOrientation90:
341 flags = Transform::ROT_90;
342 break;
343 case ISurfaceComposer::eOrientation180:
344 flags = Transform::ROT_180;
345 break;
346 case ISurfaceComposer::eOrientation270:
347 flags = Transform::ROT_270;
348 break;
349 default:
350 return BAD_VALUE;
351 }
352 tr->set(flags, w, h);
353 return NO_ERROR;
354}
355
Mathias Agopian98a121a2012-07-24 21:08:59 -0700356status_t DisplayHardware::setOrientation(int orientation) {
357 int w = mDisplayWidth;
358 int h = mDisplayHeight;
Mathias Agopian1b031492012-06-20 17:51:20 -0700359
Mathias Agopian98a121a2012-07-24 21:08:59 -0700360 DisplayHardware::orientationToTransfrom(
361 orientation, w, h, &mGlobalTransform);
Mathias Agopian1b031492012-06-20 17:51:20 -0700362 if (orientation & ISurfaceComposer::eOrientationSwapMask) {
Mathias Agopian98a121a2012-07-24 21:08:59 -0700363 int tmp = w;
364 w = h;
365 h = tmp;
Mathias Agopian1b031492012-06-20 17:51:20 -0700366 }
Mathias Agopian1b031492012-06-20 17:51:20 -0700367 mOrientation = orientation;
Mathias Agopian98a121a2012-07-24 21:08:59 -0700368
369 // update the shared control block
370 surface_flinger_cblk_t* const scblk = mFlinger->getControlBlock();
371 volatile display_cblk_t* dcblk = &scblk->displays[mDisplayId];
372 dcblk->orientation = orientation;
373 dcblk->w = w;
374 dcblk->h = h;
375
Mathias Agopian1b031492012-06-20 17:51:20 -0700376 return NO_ERROR;
377}