blob: 166c52804e10d9405f2f6a5cf34c85d925303f6f [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>
Mathias Agopian0926f502009-05-04 14:17:04 -070028#include <ui/FramebufferNativeWindow.h>
Mathias Agopian6cf50a72009-08-06 16:05:39 -070029#include <ui/EGLUtils.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080030
31#include <GLES/gl.h>
Mathias Agopian076b1cc2009-04-10 14:24:30 -070032#include <EGL/egl.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080033#include <EGL/eglext.h>
34
Mathias Agopian076b1cc2009-04-10 14:24:30 -070035#include <pixelflinger/pixelflinger.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080036
37#include "DisplayHardware/DisplayHardware.h"
38
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080039#include <hardware/overlay.h>
Mathias Agopian076b1cc2009-04-10 14:24:30 -070040#include <hardware/gralloc.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080041
Mathias Agopian1f7bec62010-06-25 18:02:21 -070042#include "GLExtensions.h"
Mathias Agopiana350ff92010-08-10 17:14:02 -070043#include "HWComposer.h"
Mathias Agopian1f7bec62010-06-25 18:02:21 -070044
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080045using namespace android;
46
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;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080056 LOGE("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{
63 EGLint error = eglGetError();
Mathias Agopiancbb288b2009-09-07 16:32:45 -070064 if (error && error != EGL_SUCCESS) {
65 LOGE("%s: EGL error 0x%04x (%s)",
Mathias Agopian0928e312009-08-07 16:38:10 -070066 token, int(error), EGLUtils::strerror(error));
Mathias Agopiancbb288b2009-09-07 16:32:45 -070067 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080068}
69
The Android Open Source Projectedbf3b62009-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 Agopian1f7bec62010-06-25 18:02:21 -070078 : DisplayHardwareBase(flinger, dpy),
Mathias Agopiana350ff92010-08-10 17:14:02 -070079 mFlags(0), mHwc(0)
The Android Open Source Projectedbf3b62009-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 Agopianca99fb82010-04-14 16:43:44 -070096uint32_t DisplayHardware::getMaxTextureSize() const { return mMaxTextureSize; }
97uint32_t DisplayHardware::getMaxViewportDims() const { return mMaxViewportDims; }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080098
99void DisplayHardware::init(uint32_t dpy)
100{
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700101 mNativeWindow = new FramebufferNativeWindow();
Mathias Agopian0928e312009-08-07 16:38:10 -0700102 framebuffer_device_t const * fbDev = mNativeWindow->getDevice();
Mathias Agopian1f7bec62010-06-25 18:02:21 -0700103 mDpiX = mNativeWindow->xdpi;
104 mDpiY = mNativeWindow->ydpi;
105 mRefreshRate = fbDev->fps;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700106
107 mOverlayEngine = NULL;
Mathias Agopian0928e312009-08-07 16:38:10 -0700108 hw_module_t const* module;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700109 if (hw_get_module(OVERLAY_HARDWARE_MODULE_ID, &module) == 0) {
110 overlay_control_open(module, &mOverlayEngine);
111 }
112
Mathias Agopian1f7bec62010-06-25 18:02:21 -0700113 EGLint w, h, dummy;
114 EGLint numConfigs=0;
115 EGLSurface surface;
116 EGLContext context;
117
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800118 // initialize EGL
Mathias Agopiana5b02e02009-09-04 18:49:03 -0700119 EGLint attribs[] = {
Mathias Agopian0928e312009-08-07 16:38:10 -0700120 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
Mathias Agopiana5b02e02009-09-04 18:49:03 -0700121 EGL_NONE, 0,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800122 EGL_NONE
123 };
Mathias Agopiana5b02e02009-09-04 18:49:03 -0700124
125 // debug: disable h/w rendering
126 char property[PROPERTY_VALUE_MAX];
127 if (property_get("debug.sf.hw", property, NULL) > 0) {
128 if (atoi(property) == 0) {
129 LOGW("H/W composition disabled");
130 attribs[2] = EGL_CONFIG_CAVEAT;
131 attribs[3] = EGL_SLOW_CONFIG;
132 }
133 }
134
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800135 // TODO: all the extensions below should be queried through
136 // eglGetProcAddress().
137
138 EGLDisplay display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
139 eglInitialize(display, NULL, NULL);
140 eglGetConfigs(display, NULL, 0, &numConfigs);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700141
Mathias Agopian6cf50a72009-08-06 16:05:39 -0700142 EGLConfig config;
Mathias Agopian0928e312009-08-07 16:38:10 -0700143 status_t err = EGLUtils::selectConfigForNativeWindow(
144 display, attribs, mNativeWindow.get(), &config);
Mathias Agopian6cf50a72009-08-06 16:05:39 -0700145 LOGE_IF(err, "couldn't find an EGLConfig matching the screen format");
146
Mathias Agopian0928e312009-08-07 16:38:10 -0700147 EGLint r,g,b,a;
148 eglGetConfigAttrib(display, config, EGL_RED_SIZE, &r);
149 eglGetConfigAttrib(display, config, EGL_GREEN_SIZE, &g);
150 eglGetConfigAttrib(display, config, EGL_BLUE_SIZE, &b);
151 eglGetConfigAttrib(display, config, EGL_ALPHA_SIZE, &a);
152
Mathias Agopian1e16b132009-05-07 17:40:23 -0700153 if (mNativeWindow->isUpdateOnDemand()) {
Mathias Agopian95a666b2009-09-24 14:57:26 -0700154 mFlags |= PARTIAL_UPDATES;
Mathias Agopian1e16b132009-05-07 17:40:23 -0700155 }
156
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800157 if (eglGetConfigAttrib(display, config, EGL_CONFIG_CAVEAT, &dummy) == EGL_TRUE) {
158 if (dummy == EGL_SLOW_CONFIG)
159 mFlags |= SLOW_CONFIG;
160 }
161
162 /*
163 * Create our main surface
164 */
165
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700166 surface = eglCreateWindowSurface(display, config, mNativeWindow.get(), NULL);
Mathias Agopian1f7bec62010-06-25 18:02:21 -0700167 eglQuerySurface(display, surface, EGL_WIDTH, &mWidth);
168 eglQuerySurface(display, surface, EGL_HEIGHT, &mHeight);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800169
Mathias Agopian95a666b2009-09-24 14:57:26 -0700170 if (mFlags & PARTIAL_UPDATES) {
171 // if we have partial updates, we definitely don't need to
172 // preserve the backbuffer, which may be costly.
Mathias Agopian0928bee2009-09-16 20:15:42 -0700173 eglSurfaceAttrib(display, surface,
174 EGL_SWAP_BEHAVIOR, EGL_BUFFER_DESTROYED);
175 }
176
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800177 if (eglQuerySurface(display, surface, EGL_SWAP_BEHAVIOR, &dummy) == EGL_TRUE) {
178 if (dummy == EGL_BUFFER_PRESERVED) {
179 mFlags |= BUFFER_PRESERVED;
180 }
181 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800182
David 'Digit' Turnerae71acc2009-06-19 04:41:12 +0200183 /* Read density from build-specific ro.sf.lcd_density property
Mathias Agopian24e5f522009-08-12 21:18:15 -0700184 * except if it is overridden by qemu.sf.lcd_density.
David 'Digit' Turnerae71acc2009-06-19 04:41:12 +0200185 */
186 if (property_get("qemu.sf.lcd_density", property, NULL) <= 0) {
187 if (property_get("ro.sf.lcd_density", property, NULL) <= 0) {
188 LOGW("ro.sf.lcd_density not defined, using 160 dpi by default.");
189 strcpy(property, "160");
David 'Digit' Turner694e10b2009-06-18 04:30:32 +0200190 }
David 'Digit' Turner31469e12009-07-29 00:38:58 +0200191 } else {
192 /* for the emulator case, reset the dpi values too */
193 mDpiX = mDpiY = atoi(property);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800194 }
195 mDensity = atoi(property) * (1.0f/160.0f);
196
197
198 /*
199 * Create our OpenGL ES context
200 */
201
202 context = eglCreateContext(display, config, NULL, NULL);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800203
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800204 mDisplay = display;
205 mConfig = config;
206 mSurface = surface;
207 mContext = context;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700208 mFormat = fbDev->format;
209 mPageFlipCount = 0;
Mathias Agopian1f7bec62010-06-25 18:02:21 -0700210
211 /*
212 * Gather OpenGL ES extensions
213 */
214
215 eglMakeCurrent(display, surface, surface, context);
216
217 GLExtensions& extensions(GLExtensions::getInstance());
218 extensions.initWithGLStrings(
219 glGetString(GL_VENDOR),
220 glGetString(GL_RENDERER),
221 glGetString(GL_VERSION),
222 glGetString(GL_EXTENSIONS),
223 eglQueryString(display, EGL_VENDOR),
224 eglQueryString(display, EGL_VERSION),
225 eglQueryString(display, EGL_EXTENSIONS));
226
227 glGetIntegerv(GL_MAX_TEXTURE_SIZE, &mMaxTextureSize);
228 glGetIntegerv(GL_MAX_VIEWPORT_DIMS, &mMaxViewportDims);
229
230
231#ifdef EGL_ANDROID_swap_rectangle
232 if (extensions.hasExtension("EGL_ANDROID_swap_rectangle")) {
233 if (eglSetSwapRectangleANDROID(display, surface,
234 0, 0, mWidth, mHeight) == EGL_TRUE) {
235 // This could fail if this extension is not supported by this
236 // specific surface (of config)
237 mFlags |= SWAP_RECTANGLE;
238 }
239 }
240 // when we have the choice between PARTIAL_UPDATES and SWAP_RECTANGLE
241 // choose PARTIAL_UPDATES, which should be more efficient
242 if (mFlags & PARTIAL_UPDATES)
243 mFlags &= ~SWAP_RECTANGLE;
244#endif
245
246 LOGI("EGL informations:");
247 LOGI("# of configs : %d", numConfigs);
248 LOGI("vendor : %s", extensions.getEglVendor());
249 LOGI("version : %s", extensions.getEglVersion());
250 LOGI("extensions: %s", extensions.getEglExtension());
251 LOGI("Client API: %s", eglQueryString(display, EGL_CLIENT_APIS)?:"Not Supported");
252 LOGI("EGLSurface: %d-%d-%d-%d, config=%p", r, g, b, a, config);
253
254 LOGI("OpenGL informations:");
255 LOGI("vendor : %s", extensions.getVendor());
256 LOGI("renderer : %s", extensions.getRenderer());
257 LOGI("version : %s", extensions.getVersion());
258 LOGI("extensions: %s", extensions.getExtension());
259 LOGI("GL_MAX_TEXTURE_SIZE = %d", mMaxTextureSize);
260 LOGI("GL_MAX_VIEWPORT_DIMS = %d", mMaxViewportDims);
261 LOGI("flags = %08x", mFlags);
262
263 // Unbind the context from this thread
264 eglMakeCurrent(display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
Mathias Agopiana350ff92010-08-10 17:14:02 -0700265
266
267 // initialize the H/W composer
268 mHwc = new HWComposer();
269 if (mHwc->initCheck() == NO_ERROR) {
270 mHwc->setFrameBuffer(mDisplay, mSurface);
271 }
272}
273
274HWComposer& DisplayHardware::getHwComposer() const {
275 return *mHwc;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800276}
277
278/*
279 * Clean up. Throw out our local state.
280 *
281 * (It's entirely possible we'll never get here, since this is meant
282 * for real hardware, which doesn't restart.)
283 */
284
285void DisplayHardware::fini()
286{
287 eglMakeCurrent(mDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
288 eglTerminate(mDisplay);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800289 overlay_control_close(mOverlayEngine);
290}
291
292void DisplayHardware::releaseScreen() const
293{
294 DisplayHardwareBase::releaseScreen();
295}
296
297void DisplayHardware::acquireScreen() const
298{
299 DisplayHardwareBase::acquireScreen();
300}
301
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800302uint32_t DisplayHardware::getPageFlipCount() const {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700303 return mPageFlipCount;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800304}
305
Mathias Agopian74faca22009-09-17 16:18:16 -0700306status_t DisplayHardware::compositionComplete() const {
307 return mNativeWindow->compositionComplete();
308}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800309
310void DisplayHardware::flip(const Region& dirty) const
311{
312 checkGLErrors();
313
314 EGLDisplay dpy = mDisplay;
315 EGLSurface surface = mSurface;
316
Mathias Agopian5e78e092009-06-11 17:19:54 -0700317#ifdef EGL_ANDROID_swap_rectangle
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700318 if (mFlags & SWAP_RECTANGLE) {
Mathias Agopianb8a55602009-06-26 19:06:36 -0700319 const Region newDirty(dirty.intersect(bounds()));
320 const Rect b(newDirty.getBounds());
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700321 eglSetSwapRectangleANDROID(dpy, surface,
322 b.left, b.top, b.width(), b.height());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800323 }
Mathias Agopian5e78e092009-06-11 17:19:54 -0700324#endif
325
Mathias Agopian95a666b2009-09-24 14:57:26 -0700326 if (mFlags & PARTIAL_UPDATES) {
Mathias Agopian29d06ac2009-06-29 18:49:56 -0700327 mNativeWindow->setUpdateRectangle(dirty.getBounds());
Mathias Agopian1e16b132009-05-07 17:40:23 -0700328 }
329
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700330 mPageFlipCount++;
Mathias Agopiana350ff92010-08-10 17:14:02 -0700331
332 if (mHwc->initCheck() == NO_ERROR) {
333 mHwc->commit();
334 } else {
335 eglSwapBuffers(dpy, surface);
336 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800337 checkEGLErrors("eglSwapBuffers");
338
339 // for debugging
340 //glClearColor(1,0,0,0);
341 //glClear(GL_COLOR_BUFFER_BIT);
342}
343
344uint32_t DisplayHardware::getFlags() const
345{
346 return mFlags;
347}
348
349void DisplayHardware::makeCurrent() const
350{
351 eglMakeCurrent(mDisplay, mSurface, mSurface, mContext);
352}