blob: d893f0ac453d430f596b220e941c8d341024b98f [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
39#include <hardware/copybit.h>
40#include <hardware/overlay.h>
Mathias Agopian076b1cc2009-04-10 14:24:30 -070041#include <hardware/gralloc.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080042
43using namespace android;
44
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080045
46static __attribute__((noinline))
47void checkGLErrors()
48{
Mathias Agopiancbb288b2009-09-07 16:32:45 -070049 do {
50 // there could be more than one error flag
51 GLenum error = glGetError();
52 if (error == GL_NO_ERROR)
53 break;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080054 LOGE("GL error 0x%04x", int(error));
Mathias Agopiancbb288b2009-09-07 16:32:45 -070055 } while(true);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080056}
57
58static __attribute__((noinline))
59void checkEGLErrors(const char* token)
60{
61 EGLint error = eglGetError();
Mathias Agopiancbb288b2009-09-07 16:32:45 -070062 if (error && error != EGL_SUCCESS) {
63 LOGE("%s: EGL error 0x%04x (%s)",
Mathias Agopian0928e312009-08-07 16:38:10 -070064 token, int(error), EGLUtils::strerror(error));
Mathias Agopiancbb288b2009-09-07 16:32:45 -070065 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080066}
67
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080068/*
69 * Initialize the display to the specified values.
70 *
71 */
72
73DisplayHardware::DisplayHardware(
74 const sp<SurfaceFlinger>& flinger,
75 uint32_t dpy)
76 : DisplayHardwareBase(flinger, dpy)
77{
78 init(dpy);
79}
80
81DisplayHardware::~DisplayHardware()
82{
83 fini();
84}
85
86float DisplayHardware::getDpiX() const { return mDpiX; }
87float DisplayHardware::getDpiY() const { return mDpiY; }
88float DisplayHardware::getDensity() const { return mDensity; }
89float DisplayHardware::getRefreshRate() const { return mRefreshRate; }
90int DisplayHardware::getWidth() const { return mWidth; }
91int DisplayHardware::getHeight() const { return mHeight; }
92PixelFormat DisplayHardware::getFormat() const { return mFormat; }
93
94void DisplayHardware::init(uint32_t dpy)
95{
Mathias Agopian076b1cc2009-04-10 14:24:30 -070096 mNativeWindow = new FramebufferNativeWindow();
Mathias Agopian0928e312009-08-07 16:38:10 -070097 framebuffer_device_t const * fbDev = mNativeWindow->getDevice();
Mathias Agopian076b1cc2009-04-10 14:24:30 -070098
99 mOverlayEngine = NULL;
Mathias Agopian0928e312009-08-07 16:38:10 -0700100 hw_module_t const* module;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700101 if (hw_get_module(OVERLAY_HARDWARE_MODULE_ID, &module) == 0) {
102 overlay_control_open(module, &mOverlayEngine);
103 }
104
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800105 // initialize EGL
Mathias Agopiana5b02e02009-09-04 18:49:03 -0700106 EGLint attribs[] = {
Mathias Agopian0928e312009-08-07 16:38:10 -0700107 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
Mathias Agopiana5b02e02009-09-04 18:49:03 -0700108 EGL_NONE, 0,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800109 EGL_NONE
110 };
Mathias Agopiana5b02e02009-09-04 18:49:03 -0700111
112 // debug: disable h/w rendering
113 char property[PROPERTY_VALUE_MAX];
114 if (property_get("debug.sf.hw", property, NULL) > 0) {
115 if (atoi(property) == 0) {
116 LOGW("H/W composition disabled");
117 attribs[2] = EGL_CONFIG_CAVEAT;
118 attribs[3] = EGL_SLOW_CONFIG;
119 }
120 }
121
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800122 EGLint w, h, dummy;
Mathias Agopian0928e312009-08-07 16:38:10 -0700123 EGLint numConfigs=0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800124 EGLSurface surface;
125 EGLContext context;
126 mFlags = 0;
127
128 // TODO: all the extensions below should be queried through
129 // eglGetProcAddress().
130
131 EGLDisplay display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
132 eglInitialize(display, NULL, NULL);
133 eglGetConfigs(display, NULL, 0, &numConfigs);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700134
Mathias Agopian6cf50a72009-08-06 16:05:39 -0700135 EGLConfig config;
Mathias Agopian0928e312009-08-07 16:38:10 -0700136 status_t err = EGLUtils::selectConfigForNativeWindow(
137 display, attribs, mNativeWindow.get(), &config);
Mathias Agopian6cf50a72009-08-06 16:05:39 -0700138 LOGE_IF(err, "couldn't find an EGLConfig matching the screen format");
139
Mathias Agopian0928e312009-08-07 16:38:10 -0700140 EGLint r,g,b,a;
141 eglGetConfigAttrib(display, config, EGL_RED_SIZE, &r);
142 eglGetConfigAttrib(display, config, EGL_GREEN_SIZE, &g);
143 eglGetConfigAttrib(display, config, EGL_BLUE_SIZE, &b);
144 eglGetConfigAttrib(display, config, EGL_ALPHA_SIZE, &a);
145
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800146 /*
147 * Gather EGL extensions
148 */
149
150 const char* const egl_extensions = eglQueryString(
151 display, EGL_EXTENSIONS);
152
153 LOGI("EGL informations:");
154 LOGI("# of configs : %d", numConfigs);
155 LOGI("vendor : %s", eglQueryString(display, EGL_VENDOR));
156 LOGI("version : %s", eglQueryString(display, EGL_VERSION));
157 LOGI("extensions: %s", egl_extensions);
158 LOGI("Client API: %s", eglQueryString(display, EGL_CLIENT_APIS)?:"Not Supported");
Mathias Agopian0928e312009-08-07 16:38:10 -0700159 LOGI("EGLSurface: %d-%d-%d-%d, config=%p", r, g, b, a, config);
160
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800161
Mathias Agopian1e16b132009-05-07 17:40:23 -0700162 if (mNativeWindow->isUpdateOnDemand()) {
163 mFlags |= UPDATE_ON_DEMAND;
164 }
165
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800166 if (eglGetConfigAttrib(display, config, EGL_CONFIG_CAVEAT, &dummy) == EGL_TRUE) {
167 if (dummy == EGL_SLOW_CONFIG)
168 mFlags |= SLOW_CONFIG;
169 }
170
171 /*
172 * Create our main surface
173 */
174
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700175 surface = eglCreateWindowSurface(display, config, mNativeWindow.get(), NULL);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800176
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 }
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700182
Mathias Agopian8458a312009-08-12 21:24:53 -0700183 eglQuerySurface(display, surface, EGL_WIDTH, &mWidth);
184 eglQuerySurface(display, surface, EGL_HEIGHT, &mHeight);
185
Mathias Agopian5e78e092009-06-11 17:19:54 -0700186#ifdef EGL_ANDROID_swap_rectangle
Mathias Agopiane6bf8b32009-05-06 23:47:08 -0700187 if (strstr(egl_extensions, "EGL_ANDROID_swap_rectangle")) {
Mathias Agopian8458a312009-08-12 21:24:53 -0700188 if (eglSetSwapRectangleANDROID(display, surface,
189 0, 0, mWidth, mHeight) == EGL_TRUE) {
190 // This could fail if this extension is not supported by this
191 // specific surface (of config)
192 mFlags |= SWAP_RECTANGLE;
193 }
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700194 }
Mathias Agopian2dd67272009-06-29 18:53:53 -0700195 // when we have the choice between UPDATE_ON_DEMAND and SWAP_RECTANGLE
196 // choose UPDATE_ON_DEMAND, which is more efficient
197 if (mFlags & UPDATE_ON_DEMAND)
198 mFlags &= ~SWAP_RECTANGLE;
Mathias Agopian5e78e092009-06-11 17:19:54 -0700199#endif
Mathias Agopian2dd67272009-06-29 18:53:53 -0700200
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700201
Mathias Agopian8458a312009-08-12 21:24:53 -0700202 LOGI("flags : %08x", mFlags);
203
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700204 mDpiX = mNativeWindow->xdpi;
Mathias Agopian80d7a762009-07-09 22:11:57 -0700205 mDpiY = mNativeWindow->ydpi;
Mathias Agopian1e16b132009-05-07 17:40:23 -0700206 mRefreshRate = fbDev->fps;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800207
David 'Digit' Turnerae71acc2009-06-19 04:41:12 +0200208 /* Read density from build-specific ro.sf.lcd_density property
Mathias Agopian24e5f522009-08-12 21:18:15 -0700209 * except if it is overridden by qemu.sf.lcd_density.
David 'Digit' Turnerae71acc2009-06-19 04:41:12 +0200210 */
211 if (property_get("qemu.sf.lcd_density", property, NULL) <= 0) {
212 if (property_get("ro.sf.lcd_density", property, NULL) <= 0) {
213 LOGW("ro.sf.lcd_density not defined, using 160 dpi by default.");
214 strcpy(property, "160");
David 'Digit' Turner694e10b2009-06-18 04:30:32 +0200215 }
David 'Digit' Turner31469e12009-07-29 00:38:58 +0200216 } else {
217 /* for the emulator case, reset the dpi values too */
218 mDpiX = mDpiY = atoi(property);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800219 }
220 mDensity = atoi(property) * (1.0f/160.0f);
221
222
223 /*
224 * Create our OpenGL ES context
225 */
226
227 context = eglCreateContext(display, config, NULL, NULL);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800228
229 /*
230 * Gather OpenGL ES extensions
231 */
232
233 eglMakeCurrent(display, surface, surface, context);
234 const char* const gl_extensions = (const char*)glGetString(GL_EXTENSIONS);
235 LOGI("OpenGL informations:");
236 LOGI("vendor : %s", glGetString(GL_VENDOR));
237 LOGI("renderer : %s", glGetString(GL_RENDERER));
238 LOGI("version : %s", glGetString(GL_VERSION));
239 LOGI("extensions: %s", gl_extensions);
240
241 if (strstr(gl_extensions, "GL_ARB_texture_non_power_of_two")) {
242 mFlags |= NPOT_EXTENSION;
243 }
244 if (strstr(gl_extensions, "GL_OES_draw_texture")) {
245 mFlags |= DRAW_TEXTURE_EXTENSION;
246 }
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700247 if (strstr( gl_extensions, "GL_OES_EGL_image") &&
Mathias Agopiane6bf8b32009-05-06 23:47:08 -0700248 (strstr(egl_extensions, "EGL_KHR_image_base") ||
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700249 strstr(egl_extensions, "EGL_KHR_image")) &&
250 strstr(egl_extensions, "EGL_ANDROID_image_native_buffer")) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800251 mFlags |= DIRECT_TEXTURE;
252 }
253
254 // Unbind the context from this thread
255 eglMakeCurrent(display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
256
257 mDisplay = display;
258 mConfig = config;
259 mSurface = surface;
260 mContext = context;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700261 mFormat = fbDev->format;
262 mPageFlipCount = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800263}
264
265/*
266 * Clean up. Throw out our local state.
267 *
268 * (It's entirely possible we'll never get here, since this is meant
269 * for real hardware, which doesn't restart.)
270 */
271
272void DisplayHardware::fini()
273{
274 eglMakeCurrent(mDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
275 eglTerminate(mDisplay);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800276 overlay_control_close(mOverlayEngine);
277}
278
279void DisplayHardware::releaseScreen() const
280{
281 DisplayHardwareBase::releaseScreen();
282}
283
284void DisplayHardware::acquireScreen() const
285{
286 DisplayHardwareBase::acquireScreen();
287}
288
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800289uint32_t DisplayHardware::getPageFlipCount() const {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700290 return mPageFlipCount;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800291}
292
293/*
294 * "Flip" the front and back buffers.
295 */
296
297void DisplayHardware::flip(const Region& dirty) const
298{
299 checkGLErrors();
300
301 EGLDisplay dpy = mDisplay;
302 EGLSurface surface = mSurface;
303
Mathias Agopian5e78e092009-06-11 17:19:54 -0700304#ifdef EGL_ANDROID_swap_rectangle
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700305 if (mFlags & SWAP_RECTANGLE) {
Mathias Agopianb8a55602009-06-26 19:06:36 -0700306 const Region newDirty(dirty.intersect(bounds()));
307 const Rect b(newDirty.getBounds());
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700308 eglSetSwapRectangleANDROID(dpy, surface,
309 b.left, b.top, b.width(), b.height());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800310 }
Mathias Agopian5e78e092009-06-11 17:19:54 -0700311#endif
312
Mathias Agopian1e16b132009-05-07 17:40:23 -0700313 if (mFlags & UPDATE_ON_DEMAND) {
Mathias Agopian29d06ac2009-06-29 18:49:56 -0700314 mNativeWindow->setUpdateRectangle(dirty.getBounds());
Mathias Agopian1e16b132009-05-07 17:40:23 -0700315 }
316
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700317 mPageFlipCount++;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800318 eglSwapBuffers(dpy, surface);
319 checkEGLErrors("eglSwapBuffers");
320
321 // for debugging
322 //glClearColor(1,0,0,0);
323 //glClear(GL_COLOR_BUFFER_BIT);
324}
325
326uint32_t DisplayHardware::getFlags() const
327{
328 return mFlags;
329}
330
331void DisplayHardware::makeCurrent() const
332{
333 eglMakeCurrent(mDisplay, mSurface, mSurface, mContext);
334}