blob: 61096e58a42024749be2d80d450713e0eea5b23f [file] [log] [blame]
The Android Open Source Project9066cfe2009-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 Project9066cfe2009-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 Agopian1473f462009-04-10 14:24:30 -070024#include <utils/RefBase.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080025#include <utils/Log.h>
26
Mathias Agopian1473f462009-04-10 14:24:30 -070027#include <ui/PixelFormat.h>
Mathias Agopiandff8e582009-05-04 14:17:04 -070028#include <ui/FramebufferNativeWindow.h>
Mathias Agopian265d9c02009-08-06 16:05:39 -070029#include <ui/EGLUtils.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080030
31#include <GLES/gl.h>
Mathias Agopian1473f462009-04-10 14:24:30 -070032#include <EGL/egl.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080033#include <EGL/eglext.h>
34
Mathias Agopian1473f462009-04-10 14:24:30 -070035#include <pixelflinger/pixelflinger.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080036
37#include "DisplayHardware/DisplayHardware.h"
38
Mathias Agopian1473f462009-04-10 14:24:30 -070039#include <hardware/gralloc.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080040
Mathias Agopian781953d2010-06-25 18:02:21 -070041#include "GLExtensions.h"
Mathias Agopiane0d5f5b2010-08-10 17:14:02 -070042#include "HWComposer.h"
Mathias Agopiana0bd0d82011-08-01 16:32:21 -070043#include "SurfaceFlinger.h"
Mathias Agopian781953d2010-06-25 18:02:21 -070044
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080045using namespace android;
46
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080047
48static __attribute__((noinline))
49void checkGLErrors()
50{
Mathias Agopian9779b222009-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 Blockc6aacce2012-01-06 19:20:56 +000056 ALOGE("GL error 0x%04x", int(error));
Mathias Agopian9779b222009-09-07 16:32:45 -070057 } while(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080058}
59
60static __attribute__((noinline))
61void checkEGLErrors(const char* token)
62{
63 EGLint error = eglGetError();
Mathias Agopian9779b222009-09-07 16:32:45 -070064 if (error && error != EGL_SUCCESS) {
Steve Blockc6aacce2012-01-06 19:20:56 +000065 ALOGE("%s: EGL error 0x%04x (%s)",
Mathias Agopian9ca4dd62009-08-07 16:38:10 -070066 token, int(error), EGLUtils::strerror(error));
Mathias Agopian9779b222009-09-07 16:32:45 -070067 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080068}
69
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080070/*
71 * Initialize the display to the specified values.
72 *
73 */
74
75DisplayHardware::DisplayHardware(
76 const sp<SurfaceFlinger>& flinger,
77 uint32_t dpy)
Mathias Agopian781953d2010-06-25 18:02:21 -070078 : DisplayHardwareBase(flinger, dpy),
Mathias Agopiana0bd0d82011-08-01 16:32:21 -070079 mFlinger(flinger), mFlags(0), mHwc(0)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080080{
81 init(dpy);
82}
83
84DisplayHardware::~DisplayHardware()
85{
86 fini();
87}
88
89float DisplayHardware::getDpiX() const { return mDpiX; }
90float DisplayHardware::getDpiY() const { return mDpiY; }
91float DisplayHardware::getDensity() const { return mDensity; }
92float DisplayHardware::getRefreshRate() const { return mRefreshRate; }
93int DisplayHardware::getWidth() const { return mWidth; }
94int DisplayHardware::getHeight() const { return mHeight; }
95PixelFormat DisplayHardware::getFormat() const { return mFormat; }
Mathias Agopian967dce32010-04-14 16:43:44 -070096uint32_t DisplayHardware::getMaxTextureSize() const { return mMaxTextureSize; }
Mathias Agopianc94dce52011-04-18 15:59:24 -070097
98uint32_t DisplayHardware::getMaxViewportDims() const {
99 return mMaxViewportDims[0] < mMaxViewportDims[1] ?
100 mMaxViewportDims[0] : mMaxViewportDims[1];
101}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800102
Mathias Agopian18617862011-07-06 16:35:30 -0700103static status_t selectConfigForPixelFormat(
104 EGLDisplay dpy,
105 EGLint const* attrs,
106 PixelFormat format,
107 EGLConfig* outConfig)
108{
109 EGLConfig config = NULL;
110 EGLint numConfigs = -1, n=0;
111 eglGetConfigs(dpy, NULL, 0, &numConfigs);
112 EGLConfig* const configs = new EGLConfig[numConfigs];
113 eglChooseConfig(dpy, attrs, configs, numConfigs, &n);
114 for (int i=0 ; i<n ; i++) {
115 EGLint nativeVisualId = 0;
116 eglGetConfigAttrib(dpy, configs[i], EGL_NATIVE_VISUAL_ID, &nativeVisualId);
117 if (nativeVisualId>0 && format == nativeVisualId) {
118 *outConfig = configs[i];
119 delete [] configs;
120 return NO_ERROR;
121 }
122 }
123 delete [] configs;
124 return NAME_NOT_FOUND;
125}
126
127
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800128void DisplayHardware::init(uint32_t dpy)
129{
Mathias Agopian1473f462009-04-10 14:24:30 -0700130 mNativeWindow = new FramebufferNativeWindow();
Mathias Agopian9ca4dd62009-08-07 16:38:10 -0700131 framebuffer_device_t const * fbDev = mNativeWindow->getDevice();
Mathias Agopian0c63ef52011-07-01 17:08:43 -0700132 if (!fbDev) {
Steve Blockc6aacce2012-01-06 19:20:56 +0000133 ALOGE("Display subsystem failed to initialize. check logs. exiting...");
Mathias Agopian0c63ef52011-07-01 17:08:43 -0700134 exit(0);
135 }
136
Mathias Agopian18617862011-07-06 16:35:30 -0700137 int format;
138 ANativeWindow const * const window = mNativeWindow.get();
139 window->query(window, NATIVE_WINDOW_FORMAT, &format);
Mathias Agopian781953d2010-06-25 18:02:21 -0700140 mDpiX = mNativeWindow->xdpi;
141 mDpiY = mNativeWindow->ydpi;
142 mRefreshRate = fbDev->fps;
Mathias Agopian1473f462009-04-10 14:24:30 -0700143
Mathias Agopian222357f2011-11-04 18:46:11 -0700144
145/* FIXME: this is a temporary HACK until we are able to report the refresh rate
146 * properly from the HAL. The WindowManagerService now relies on this value.
147 */
148#ifndef REFRESH_RATE
149 mRefreshRate = fbDev->fps;
150#else
151 mRefreshRate = REFRESH_RATE;
152#warning "refresh rate set via makefile to REFRESH_RATE"
153#endif
154
Mathias Agopian781953d2010-06-25 18:02:21 -0700155 EGLint w, h, dummy;
156 EGLint numConfigs=0;
157 EGLSurface surface;
158 EGLContext context;
Mathias Agopian18617862011-07-06 16:35:30 -0700159 EGLBoolean result;
160 status_t err;
Mathias Agopian781953d2010-06-25 18:02:21 -0700161
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800162 // initialize EGL
Mathias Agopian24d237d2009-09-04 18:49:03 -0700163 EGLint attribs[] = {
Mathias Agopian18617862011-07-06 16:35:30 -0700164 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
165 EGL_NONE, 0,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800166 EGL_NONE
167 };
Mathias Agopian24d237d2009-09-04 18:49:03 -0700168
169 // debug: disable h/w rendering
170 char property[PROPERTY_VALUE_MAX];
171 if (property_get("debug.sf.hw", property, NULL) > 0) {
172 if (atoi(property) == 0) {
Steve Blocka51f0e72012-01-05 23:22:43 +0000173 ALOGW("H/W composition disabled");
Mathias Agopian24d237d2009-09-04 18:49:03 -0700174 attribs[2] = EGL_CONFIG_CAVEAT;
175 attribs[3] = EGL_SLOW_CONFIG;
176 }
177 }
178
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800179 // TODO: all the extensions below should be queried through
180 // eglGetProcAddress().
181
182 EGLDisplay display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
183 eglInitialize(display, NULL, NULL);
184 eglGetConfigs(display, NULL, 0, &numConfigs);
Mathias Agopian1473f462009-04-10 14:24:30 -0700185
Mathias Agopian18617862011-07-06 16:35:30 -0700186 EGLConfig config = NULL;
187 err = selectConfigForPixelFormat(display, attribs, format, &config);
Steve Blockc6aacce2012-01-06 19:20:56 +0000188 ALOGE_IF(err, "couldn't find an EGLConfig matching the screen format");
Mathias Agopian265d9c02009-08-06 16:05:39 -0700189
Mathias Agopian9ca4dd62009-08-07 16:38:10 -0700190 EGLint r,g,b,a;
191 eglGetConfigAttrib(display, config, EGL_RED_SIZE, &r);
192 eglGetConfigAttrib(display, config, EGL_GREEN_SIZE, &g);
193 eglGetConfigAttrib(display, config, EGL_BLUE_SIZE, &b);
194 eglGetConfigAttrib(display, config, EGL_ALPHA_SIZE, &a);
195
Mathias Agopian97b80562009-05-07 17:40:23 -0700196 if (mNativeWindow->isUpdateOnDemand()) {
Mathias Agopianf2d28b72009-09-24 14:57:26 -0700197 mFlags |= PARTIAL_UPDATES;
Mathias Agopian97b80562009-05-07 17:40:23 -0700198 }
199
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800200 if (eglGetConfigAttrib(display, config, EGL_CONFIG_CAVEAT, &dummy) == EGL_TRUE) {
201 if (dummy == EGL_SLOW_CONFIG)
202 mFlags |= SLOW_CONFIG;
203 }
204
205 /*
206 * Create our main surface
207 */
208
Mathias Agopian1473f462009-04-10 14:24:30 -0700209 surface = eglCreateWindowSurface(display, config, mNativeWindow.get(), NULL);
Mathias Agopian781953d2010-06-25 18:02:21 -0700210 eglQuerySurface(display, surface, EGL_WIDTH, &mWidth);
211 eglQuerySurface(display, surface, EGL_HEIGHT, &mHeight);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800212
Mathias Agopianf2d28b72009-09-24 14:57:26 -0700213 if (mFlags & PARTIAL_UPDATES) {
214 // if we have partial updates, we definitely don't need to
215 // preserve the backbuffer, which may be costly.
Mathias Agopianaf369f622009-09-16 20:15:42 -0700216 eglSurfaceAttrib(display, surface,
217 EGL_SWAP_BEHAVIOR, EGL_BUFFER_DESTROYED);
218 }
219
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800220 if (eglQuerySurface(display, surface, EGL_SWAP_BEHAVIOR, &dummy) == EGL_TRUE) {
221 if (dummy == EGL_BUFFER_PRESERVED) {
222 mFlags |= BUFFER_PRESERVED;
223 }
224 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800225
David 'Digit' Turner3c08a442009-06-19 04:41:12 +0200226 /* Read density from build-specific ro.sf.lcd_density property
Mathias Agopian88e3e6b2009-08-12 21:18:15 -0700227 * except if it is overridden by qemu.sf.lcd_density.
David 'Digit' Turner3c08a442009-06-19 04:41:12 +0200228 */
229 if (property_get("qemu.sf.lcd_density", property, NULL) <= 0) {
230 if (property_get("ro.sf.lcd_density", property, NULL) <= 0) {
Steve Blocka51f0e72012-01-05 23:22:43 +0000231 ALOGW("ro.sf.lcd_density not defined, using 160 dpi by default.");
David 'Digit' Turner3c08a442009-06-19 04:41:12 +0200232 strcpy(property, "160");
David 'Digit' Turner2a578ae52009-06-18 04:30:32 +0200233 }
David 'Digit' Turnerd75fe9a2009-07-29 00:38:58 +0200234 } else {
235 /* for the emulator case, reset the dpi values too */
236 mDpiX = mDpiY = atoi(property);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800237 }
238 mDensity = atoi(property) * (1.0f/160.0f);
239
240
241 /*
242 * Create our OpenGL ES context
243 */
244
Mathias Agopiancd54dc22010-10-11 17:54:43 -0700245
246 EGLint contextAttributes[] = {
247#ifdef EGL_IMG_context_priority
248#ifdef HAS_CONTEXT_PRIORITY
249#warning "using EGL_IMG_context_priority"
250 EGL_CONTEXT_PRIORITY_LEVEL_IMG, EGL_CONTEXT_PRIORITY_HIGH_IMG,
251#endif
252#endif
253 EGL_NONE, EGL_NONE
254 };
255 context = eglCreateContext(display, config, NULL, contextAttributes);
256
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800257 mDisplay = display;
258 mConfig = config;
259 mSurface = surface;
260 mContext = context;
Mathias Agopian1473f462009-04-10 14:24:30 -0700261 mFormat = fbDev->format;
262 mPageFlipCount = 0;
Mathias Agopian781953d2010-06-25 18:02:21 -0700263
264 /*
265 * Gather OpenGL ES extensions
266 */
267
Mathias Agopian18617862011-07-06 16:35:30 -0700268 result = eglMakeCurrent(display, surface, surface, context);
269 if (!result) {
Steve Blockc6aacce2012-01-06 19:20:56 +0000270 ALOGE("Couldn't create a working GLES context. check logs. exiting...");
Mathias Agopian18617862011-07-06 16:35:30 -0700271 exit(0);
272 }
Mathias Agopian781953d2010-06-25 18:02:21 -0700273
274 GLExtensions& extensions(GLExtensions::getInstance());
275 extensions.initWithGLStrings(
276 glGetString(GL_VENDOR),
277 glGetString(GL_RENDERER),
278 glGetString(GL_VERSION),
279 glGetString(GL_EXTENSIONS),
280 eglQueryString(display, EGL_VENDOR),
281 eglQueryString(display, EGL_VERSION),
282 eglQueryString(display, EGL_EXTENSIONS));
283
284 glGetIntegerv(GL_MAX_TEXTURE_SIZE, &mMaxTextureSize);
Mathias Agopianc94dce52011-04-18 15:59:24 -0700285 glGetIntegerv(GL_MAX_VIEWPORT_DIMS, mMaxViewportDims);
Mathias Agopian781953d2010-06-25 18:02:21 -0700286
Steve Block933e8562012-01-04 20:05:49 +0000287 ALOGI("EGL informations:");
288 ALOGI("# of configs : %d", numConfigs);
289 ALOGI("vendor : %s", extensions.getEglVendor());
290 ALOGI("version : %s", extensions.getEglVersion());
291 ALOGI("extensions: %s", extensions.getEglExtension());
292 ALOGI("Client API: %s", eglQueryString(display, EGL_CLIENT_APIS)?:"Not Supported");
293 ALOGI("EGLSurface: %d-%d-%d-%d, config=%p", r, g, b, a, config);
Mathias Agopian781953d2010-06-25 18:02:21 -0700294
Steve Block933e8562012-01-04 20:05:49 +0000295 ALOGI("OpenGL informations:");
296 ALOGI("vendor : %s", extensions.getVendor());
297 ALOGI("renderer : %s", extensions.getRenderer());
298 ALOGI("version : %s", extensions.getVersion());
299 ALOGI("extensions: %s", extensions.getExtension());
300 ALOGI("GL_MAX_TEXTURE_SIZE = %d", mMaxTextureSize);
301 ALOGI("GL_MAX_VIEWPORT_DIMS = %d x %d", mMaxViewportDims[0], mMaxViewportDims[1]);
302 ALOGI("flags = %08x", mFlags);
Mathias Agopian781953d2010-06-25 18:02:21 -0700303
304 // Unbind the context from this thread
305 eglMakeCurrent(display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
Mathias Agopiane0d5f5b2010-08-10 17:14:02 -0700306
307
308 // initialize the H/W composer
Mathias Agopiana0bd0d82011-08-01 16:32:21 -0700309 mHwc = new HWComposer(mFlinger);
Mathias Agopiane0d5f5b2010-08-10 17:14:02 -0700310 if (mHwc->initCheck() == NO_ERROR) {
311 mHwc->setFrameBuffer(mDisplay, mSurface);
312 }
313}
314
315HWComposer& DisplayHardware::getHwComposer() const {
316 return *mHwc;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800317}
318
319/*
320 * Clean up. Throw out our local state.
321 *
322 * (It's entirely possible we'll never get here, since this is meant
323 * for real hardware, which doesn't restart.)
324 */
325
326void DisplayHardware::fini()
327{
328 eglMakeCurrent(mDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
329 eglTerminate(mDisplay);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800330}
331
332void DisplayHardware::releaseScreen() const
333{
334 DisplayHardwareBase::releaseScreen();
Antti Hatala11042a42010-09-09 02:33:05 -0700335 if (mHwc->initCheck() == NO_ERROR) {
336 mHwc->release();
337 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800338}
339
340void DisplayHardware::acquireScreen() const
341{
342 DisplayHardwareBase::acquireScreen();
343}
344
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800345uint32_t DisplayHardware::getPageFlipCount() const {
Mathias Agopian1473f462009-04-10 14:24:30 -0700346 return mPageFlipCount;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800347}
348
Mathias Agopianb1a18742009-09-17 16:18:16 -0700349status_t DisplayHardware::compositionComplete() const {
350 return mNativeWindow->compositionComplete();
351}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800352
Mathias Agopian04262e92010-09-13 22:57:58 -0700353int DisplayHardware::getCurrentBufferIndex() const {
354 return mNativeWindow->getCurrentBufferIndex();
355}
356
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800357void DisplayHardware::flip(const Region& dirty) const
358{
359 checkGLErrors();
360
361 EGLDisplay dpy = mDisplay;
362 EGLSurface surface = mSurface;
363
Mathias Agopiane1947622009-06-11 17:19:54 -0700364#ifdef EGL_ANDROID_swap_rectangle
Mathias Agopian2e20bff2009-05-04 19:29:25 -0700365 if (mFlags & SWAP_RECTANGLE) {
Mathias Agopian8c9687a2009-06-26 19:06:36 -0700366 const Region newDirty(dirty.intersect(bounds()));
367 const Rect b(newDirty.getBounds());
Mathias Agopian2e20bff2009-05-04 19:29:25 -0700368 eglSetSwapRectangleANDROID(dpy, surface,
369 b.left, b.top, b.width(), b.height());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800370 }
Mathias Agopiane1947622009-06-11 17:19:54 -0700371#endif
372
Mathias Agopianf2d28b72009-09-24 14:57:26 -0700373 if (mFlags & PARTIAL_UPDATES) {
Mathias Agopianecfa7cc2009-06-29 18:49:56 -0700374 mNativeWindow->setUpdateRectangle(dirty.getBounds());
Mathias Agopian97b80562009-05-07 17:40:23 -0700375 }
376
Mathias Agopian1473f462009-04-10 14:24:30 -0700377 mPageFlipCount++;
Mathias Agopiane0d5f5b2010-08-10 17:14:02 -0700378
379 if (mHwc->initCheck() == NO_ERROR) {
380 mHwc->commit();
381 } else {
382 eglSwapBuffers(dpy, surface);
383 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800384 checkEGLErrors("eglSwapBuffers");
385
386 // for debugging
387 //glClearColor(1,0,0,0);
388 //glClear(GL_COLOR_BUFFER_BIT);
389}
390
391uint32_t DisplayHardware::getFlags() const
392{
393 return mFlags;
394}
395
396void DisplayHardware::makeCurrent() const
397{
398 eglMakeCurrent(mDisplay, mSurface, mSurface, mContext);
399}
Erik Gilling94720d72010-12-01 16:38:01 -0800400
401void DisplayHardware::dump(String8& res) const
402{
403 mNativeWindow->dump(res);
404}