blob: 651e7cf16b71ab13f5e7b0ce67414fab9100026e [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
106 const EGLint attribs[] = {
Mathias Agopian0928e312009-08-07 16:38:10 -0700107 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800108 EGL_NONE
109 };
110 EGLint w, h, dummy;
Mathias Agopian0928e312009-08-07 16:38:10 -0700111 EGLint numConfigs=0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800112 EGLSurface surface;
113 EGLContext context;
114 mFlags = 0;
115
116 // TODO: all the extensions below should be queried through
117 // eglGetProcAddress().
118
119 EGLDisplay display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
120 eglInitialize(display, NULL, NULL);
121 eglGetConfigs(display, NULL, 0, &numConfigs);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700122
Mathias Agopian6cf50a72009-08-06 16:05:39 -0700123 EGLConfig config;
Mathias Agopian0928e312009-08-07 16:38:10 -0700124 status_t err = EGLUtils::selectConfigForNativeWindow(
125 display, attribs, mNativeWindow.get(), &config);
Mathias Agopian6cf50a72009-08-06 16:05:39 -0700126 LOGE_IF(err, "couldn't find an EGLConfig matching the screen format");
127
Mathias Agopian0928e312009-08-07 16:38:10 -0700128 EGLint r,g,b,a;
129 eglGetConfigAttrib(display, config, EGL_RED_SIZE, &r);
130 eglGetConfigAttrib(display, config, EGL_GREEN_SIZE, &g);
131 eglGetConfigAttrib(display, config, EGL_BLUE_SIZE, &b);
132 eglGetConfigAttrib(display, config, EGL_ALPHA_SIZE, &a);
133
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800134 /*
135 * Gather EGL extensions
136 */
137
138 const char* const egl_extensions = eglQueryString(
139 display, EGL_EXTENSIONS);
140
141 LOGI("EGL informations:");
142 LOGI("# of configs : %d", numConfigs);
143 LOGI("vendor : %s", eglQueryString(display, EGL_VENDOR));
144 LOGI("version : %s", eglQueryString(display, EGL_VERSION));
145 LOGI("extensions: %s", egl_extensions);
146 LOGI("Client API: %s", eglQueryString(display, EGL_CLIENT_APIS)?:"Not Supported");
Mathias Agopian0928e312009-08-07 16:38:10 -0700147 LOGI("EGLSurface: %d-%d-%d-%d, config=%p", r, g, b, a, config);
148
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800149
Mathias Agopian1e16b132009-05-07 17:40:23 -0700150 if (mNativeWindow->isUpdateOnDemand()) {
151 mFlags |= UPDATE_ON_DEMAND;
152 }
153
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800154 if (eglGetConfigAttrib(display, config, EGL_CONFIG_CAVEAT, &dummy) == EGL_TRUE) {
155 if (dummy == EGL_SLOW_CONFIG)
156 mFlags |= SLOW_CONFIG;
157 }
158
159 /*
160 * Create our main surface
161 */
162
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700163 surface = eglCreateWindowSurface(display, config, mNativeWindow.get(), NULL);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800164
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800165 if (eglQuerySurface(display, surface, EGL_SWAP_BEHAVIOR, &dummy) == EGL_TRUE) {
166 if (dummy == EGL_BUFFER_PRESERVED) {
167 mFlags |= BUFFER_PRESERVED;
168 }
169 }
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700170
Mathias Agopian8458a312009-08-12 21:24:53 -0700171 eglQuerySurface(display, surface, EGL_WIDTH, &mWidth);
172 eglQuerySurface(display, surface, EGL_HEIGHT, &mHeight);
173
Mathias Agopian5e78e092009-06-11 17:19:54 -0700174#ifdef EGL_ANDROID_swap_rectangle
Mathias Agopiane6bf8b32009-05-06 23:47:08 -0700175 if (strstr(egl_extensions, "EGL_ANDROID_swap_rectangle")) {
Mathias Agopian8458a312009-08-12 21:24:53 -0700176 if (eglSetSwapRectangleANDROID(display, surface,
177 0, 0, mWidth, mHeight) == EGL_TRUE) {
178 // This could fail if this extension is not supported by this
179 // specific surface (of config)
180 mFlags |= SWAP_RECTANGLE;
181 }
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700182 }
Mathias Agopian2dd67272009-06-29 18:53:53 -0700183 // when we have the choice between UPDATE_ON_DEMAND and SWAP_RECTANGLE
184 // choose UPDATE_ON_DEMAND, which is more efficient
185 if (mFlags & UPDATE_ON_DEMAND)
186 mFlags &= ~SWAP_RECTANGLE;
Mathias Agopian5e78e092009-06-11 17:19:54 -0700187#endif
Mathias Agopian2dd67272009-06-29 18:53:53 -0700188
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700189
Mathias Agopian8458a312009-08-12 21:24:53 -0700190 LOGI("flags : %08x", mFlags);
191
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700192 mDpiX = mNativeWindow->xdpi;
Mathias Agopian80d7a762009-07-09 22:11:57 -0700193 mDpiY = mNativeWindow->ydpi;
Mathias Agopian1e16b132009-05-07 17:40:23 -0700194 mRefreshRate = fbDev->fps;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800195
196 char property[PROPERTY_VALUE_MAX];
David 'Digit' Turnerae71acc2009-06-19 04:41:12 +0200197 /* Read density from build-specific ro.sf.lcd_density property
Mathias Agopian24e5f522009-08-12 21:18:15 -0700198 * except if it is overridden by qemu.sf.lcd_density.
David 'Digit' Turnerae71acc2009-06-19 04:41:12 +0200199 */
200 if (property_get("qemu.sf.lcd_density", property, NULL) <= 0) {
201 if (property_get("ro.sf.lcd_density", property, NULL) <= 0) {
202 LOGW("ro.sf.lcd_density not defined, using 160 dpi by default.");
203 strcpy(property, "160");
David 'Digit' Turner694e10b2009-06-18 04:30:32 +0200204 }
David 'Digit' Turner31469e12009-07-29 00:38:58 +0200205 } else {
206 /* for the emulator case, reset the dpi values too */
207 mDpiX = mDpiY = atoi(property);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800208 }
209 mDensity = atoi(property) * (1.0f/160.0f);
210
211
212 /*
213 * Create our OpenGL ES context
214 */
215
216 context = eglCreateContext(display, config, NULL, NULL);
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}