blob: 3f607f6dbb1d08cf7c61cf05b1187e60d384ceeb [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{
49 GLenum error = glGetError();
50 if (error != GL_NO_ERROR)
51 LOGE("GL error 0x%04x", int(error));
52}
53
54static __attribute__((noinline))
55void checkEGLErrors(const char* token)
56{
57 EGLint error = eglGetError();
58 // GLESonGL seems to be returning 0 when there is no errors?
59 if (error && error != EGL_SUCCESS)
60 LOGE("%s error 0x%04x (%s)",
Mathias Agopian0928e312009-08-07 16:38:10 -070061 token, int(error), EGLUtils::strerror(error));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080062}
63
64
65/*
66 * Initialize the display to the specified values.
67 *
68 */
69
70DisplayHardware::DisplayHardware(
71 const sp<SurfaceFlinger>& flinger,
72 uint32_t dpy)
73 : DisplayHardwareBase(flinger, dpy)
74{
75 init(dpy);
76}
77
78DisplayHardware::~DisplayHardware()
79{
80 fini();
81}
82
83float DisplayHardware::getDpiX() const { return mDpiX; }
84float DisplayHardware::getDpiY() const { return mDpiY; }
85float DisplayHardware::getDensity() const { return mDensity; }
86float DisplayHardware::getRefreshRate() const { return mRefreshRate; }
87int DisplayHardware::getWidth() const { return mWidth; }
88int DisplayHardware::getHeight() const { return mHeight; }
89PixelFormat DisplayHardware::getFormat() const { return mFormat; }
90
91void DisplayHardware::init(uint32_t dpy)
92{
Mathias Agopian076b1cc2009-04-10 14:24:30 -070093 mNativeWindow = new FramebufferNativeWindow();
Mathias Agopian0928e312009-08-07 16:38:10 -070094 framebuffer_device_t const * fbDev = mNativeWindow->getDevice();
Mathias Agopian076b1cc2009-04-10 14:24:30 -070095
96 mOverlayEngine = NULL;
Mathias Agopian0928e312009-08-07 16:38:10 -070097 hw_module_t const* module;
Mathias Agopian076b1cc2009-04-10 14:24:30 -070098 if (hw_get_module(OVERLAY_HARDWARE_MODULE_ID, &module) == 0) {
99 overlay_control_open(module, &mOverlayEngine);
100 }
101
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800102 // initialize EGL
103 const EGLint attribs[] = {
Mathias Agopian0928e312009-08-07 16:38:10 -0700104 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800105 EGL_NONE
106 };
107 EGLint w, h, dummy;
Mathias Agopian0928e312009-08-07 16:38:10 -0700108 EGLint numConfigs=0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800109 EGLSurface surface;
110 EGLContext context;
111 mFlags = 0;
112
113 // TODO: all the extensions below should be queried through
114 // eglGetProcAddress().
115
116 EGLDisplay display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
117 eglInitialize(display, NULL, NULL);
118 eglGetConfigs(display, NULL, 0, &numConfigs);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700119
Mathias Agopian6cf50a72009-08-06 16:05:39 -0700120 EGLConfig config;
Mathias Agopian0928e312009-08-07 16:38:10 -0700121 status_t err = EGLUtils::selectConfigForNativeWindow(
122 display, attribs, mNativeWindow.get(), &config);
Mathias Agopian6cf50a72009-08-06 16:05:39 -0700123 LOGE_IF(err, "couldn't find an EGLConfig matching the screen format");
124
Mathias Agopian0928e312009-08-07 16:38:10 -0700125 EGLint r,g,b,a;
126 eglGetConfigAttrib(display, config, EGL_RED_SIZE, &r);
127 eglGetConfigAttrib(display, config, EGL_GREEN_SIZE, &g);
128 eglGetConfigAttrib(display, config, EGL_BLUE_SIZE, &b);
129 eglGetConfigAttrib(display, config, EGL_ALPHA_SIZE, &a);
130
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800131 /*
132 * Gather EGL extensions
133 */
134
135 const char* const egl_extensions = eglQueryString(
136 display, EGL_EXTENSIONS);
137
138 LOGI("EGL informations:");
139 LOGI("# of configs : %d", numConfigs);
140 LOGI("vendor : %s", eglQueryString(display, EGL_VENDOR));
141 LOGI("version : %s", eglQueryString(display, EGL_VERSION));
142 LOGI("extensions: %s", egl_extensions);
143 LOGI("Client API: %s", eglQueryString(display, EGL_CLIENT_APIS)?:"Not Supported");
Mathias Agopian0928e312009-08-07 16:38:10 -0700144 LOGI("EGLSurface: %d-%d-%d-%d, config=%p", r, g, b, a, config);
145
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800146
Mathias Agopian1e16b132009-05-07 17:40:23 -0700147 if (mNativeWindow->isUpdateOnDemand()) {
148 mFlags |= UPDATE_ON_DEMAND;
149 }
150
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800151 if (eglGetConfigAttrib(display, config, EGL_CONFIG_CAVEAT, &dummy) == EGL_TRUE) {
152 if (dummy == EGL_SLOW_CONFIG)
153 mFlags |= SLOW_CONFIG;
154 }
155
156 /*
157 * Create our main surface
158 */
159
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700160 surface = eglCreateWindowSurface(display, config, mNativeWindow.get(), NULL);
Mathias Agopian5e78e092009-06-11 17:19:54 -0700161 checkEGLErrors("eglCreateWindowSurface");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800162
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800163 if (eglQuerySurface(display, surface, EGL_SWAP_BEHAVIOR, &dummy) == EGL_TRUE) {
164 if (dummy == EGL_BUFFER_PRESERVED) {
165 mFlags |= BUFFER_PRESERVED;
166 }
167 }
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700168
Mathias Agopian8458a312009-08-12 21:24:53 -0700169 eglQuerySurface(display, surface, EGL_WIDTH, &mWidth);
170 eglQuerySurface(display, surface, EGL_HEIGHT, &mHeight);
171
Mathias Agopian5e78e092009-06-11 17:19:54 -0700172#ifdef EGL_ANDROID_swap_rectangle
Mathias Agopiane6bf8b32009-05-06 23:47:08 -0700173 if (strstr(egl_extensions, "EGL_ANDROID_swap_rectangle")) {
Mathias Agopian8458a312009-08-12 21:24:53 -0700174 if (eglSetSwapRectangleANDROID(display, surface,
175 0, 0, mWidth, mHeight) == EGL_TRUE) {
176 // This could fail if this extension is not supported by this
177 // specific surface (of config)
178 mFlags |= SWAP_RECTANGLE;
179 }
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700180 }
Mathias Agopian2dd67272009-06-29 18:53:53 -0700181 // when we have the choice between UPDATE_ON_DEMAND and SWAP_RECTANGLE
182 // choose UPDATE_ON_DEMAND, which is more efficient
183 if (mFlags & UPDATE_ON_DEMAND)
184 mFlags &= ~SWAP_RECTANGLE;
Mathias Agopian5e78e092009-06-11 17:19:54 -0700185#endif
Mathias Agopian2dd67272009-06-29 18:53:53 -0700186
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700187
Mathias Agopian8458a312009-08-12 21:24:53 -0700188 LOGI("flags : %08x", mFlags);
189
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700190 mDpiX = mNativeWindow->xdpi;
Mathias Agopian80d7a762009-07-09 22:11:57 -0700191 mDpiY = mNativeWindow->ydpi;
Mathias Agopian1e16b132009-05-07 17:40:23 -0700192 mRefreshRate = fbDev->fps;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800193
194 char property[PROPERTY_VALUE_MAX];
David 'Digit' Turnerae71acc2009-06-19 04:41:12 +0200195 /* Read density from build-specific ro.sf.lcd_density property
Mathias Agopian24e5f522009-08-12 21:18:15 -0700196 * except if it is overridden by qemu.sf.lcd_density.
David 'Digit' Turnerae71acc2009-06-19 04:41:12 +0200197 */
198 if (property_get("qemu.sf.lcd_density", property, NULL) <= 0) {
199 if (property_get("ro.sf.lcd_density", property, NULL) <= 0) {
200 LOGW("ro.sf.lcd_density not defined, using 160 dpi by default.");
201 strcpy(property, "160");
David 'Digit' Turner694e10b2009-06-18 04:30:32 +0200202 }
David 'Digit' Turner31469e12009-07-29 00:38:58 +0200203 } else {
204 /* for the emulator case, reset the dpi values too */
205 mDpiX = mDpiY = atoi(property);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800206 }
207 mDensity = atoi(property) * (1.0f/160.0f);
208
209
210 /*
211 * Create our OpenGL ES context
212 */
213
214 context = eglCreateContext(display, config, NULL, NULL);
215 //checkEGLErrors("eglCreateContext");
216
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800217
218 /*
219 * Gather OpenGL ES extensions
220 */
221
222 eglMakeCurrent(display, surface, surface, context);
223 const char* const gl_extensions = (const char*)glGetString(GL_EXTENSIONS);
224 LOGI("OpenGL informations:");
225 LOGI("vendor : %s", glGetString(GL_VENDOR));
226 LOGI("renderer : %s", glGetString(GL_RENDERER));
227 LOGI("version : %s", glGetString(GL_VERSION));
228 LOGI("extensions: %s", gl_extensions);
229
230 if (strstr(gl_extensions, "GL_ARB_texture_non_power_of_two")) {
231 mFlags |= NPOT_EXTENSION;
232 }
233 if (strstr(gl_extensions, "GL_OES_draw_texture")) {
234 mFlags |= DRAW_TEXTURE_EXTENSION;
235 }
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700236 if (strstr( gl_extensions, "GL_OES_EGL_image") &&
Mathias Agopiane6bf8b32009-05-06 23:47:08 -0700237 (strstr(egl_extensions, "EGL_KHR_image_base") ||
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700238 strstr(egl_extensions, "EGL_KHR_image")) &&
239 strstr(egl_extensions, "EGL_ANDROID_image_native_buffer")) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800240 mFlags |= DIRECT_TEXTURE;
241 }
242
243 // Unbind the context from this thread
244 eglMakeCurrent(display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
245
246 mDisplay = display;
247 mConfig = config;
248 mSurface = surface;
249 mContext = context;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700250 mFormat = fbDev->format;
251 mPageFlipCount = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800252}
253
254/*
255 * Clean up. Throw out our local state.
256 *
257 * (It's entirely possible we'll never get here, since this is meant
258 * for real hardware, which doesn't restart.)
259 */
260
261void DisplayHardware::fini()
262{
263 eglMakeCurrent(mDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
264 eglTerminate(mDisplay);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800265 overlay_control_close(mOverlayEngine);
266}
267
268void DisplayHardware::releaseScreen() const
269{
270 DisplayHardwareBase::releaseScreen();
271}
272
273void DisplayHardware::acquireScreen() const
274{
275 DisplayHardwareBase::acquireScreen();
276}
277
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800278uint32_t DisplayHardware::getPageFlipCount() const {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700279 return mPageFlipCount;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800280}
281
282/*
283 * "Flip" the front and back buffers.
284 */
285
286void DisplayHardware::flip(const Region& dirty) const
287{
288 checkGLErrors();
289
290 EGLDisplay dpy = mDisplay;
291 EGLSurface surface = mSurface;
292
Mathias Agopian5e78e092009-06-11 17:19:54 -0700293#ifdef EGL_ANDROID_swap_rectangle
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700294 if (mFlags & SWAP_RECTANGLE) {
Mathias Agopianb8a55602009-06-26 19:06:36 -0700295 const Region newDirty(dirty.intersect(bounds()));
296 const Rect b(newDirty.getBounds());
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700297 eglSetSwapRectangleANDROID(dpy, surface,
298 b.left, b.top, b.width(), b.height());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800299 }
Mathias Agopian5e78e092009-06-11 17:19:54 -0700300#endif
301
Mathias Agopian1e16b132009-05-07 17:40:23 -0700302 if (mFlags & UPDATE_ON_DEMAND) {
Mathias Agopian29d06ac2009-06-29 18:49:56 -0700303 mNativeWindow->setUpdateRectangle(dirty.getBounds());
Mathias Agopian1e16b132009-05-07 17:40:23 -0700304 }
305
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700306 mPageFlipCount++;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800307 eglSwapBuffers(dpy, surface);
308 checkEGLErrors("eglSwapBuffers");
309
310 // for debugging
311 //glClearColor(1,0,0,0);
312 //glClear(GL_COLOR_BUFFER_BIT);
313}
314
315uint32_t DisplayHardware::getFlags() const
316{
317 return mFlags;
318}
319
320void DisplayHardware::makeCurrent() const
321{
322 eglMakeCurrent(mDisplay, mSurface, mSurface, mContext);
323}