blob: 1abfd68c76547d630279f9e7bb6bfe41f590a427 [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
Jean-Baptiste Querucc8c35c2009-11-12 18:45:53 -080024#include <utils/RefBase.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080025#include <utils/Log.h>
26
Jean-Baptiste Querucc8c35c2009-11-12 18:45:53 -080027#include <ui/PixelFormat.h>
28#include <ui/FramebufferNativeWindow.h>
29#include <ui/EGLUtils.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080030
31#include <GLES/gl.h>
Jean-Baptiste Querucc8c35c2009-11-12 18:45:53 -080032#include <EGL/egl.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080033#include <EGL/eglext.h>
34
Jean-Baptiste Querucc8c35c2009-11-12 18:45:53 -080035#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>
Jean-Baptiste Querucc8c35c2009-11-12 18:45:53 -080041#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{
Jean-Baptiste Querucc8c35c2009-11-12 18:45:53 -080049 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));
Jean-Baptiste Querucc8c35c2009-11-12 18:45:53 -080055 } 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();
Jean-Baptiste Querucc8c35c2009-11-12 18:45:53 -080062 if (error && error != EGL_SUCCESS) {
63 LOGE("%s: EGL error 0x%04x (%s)",
64 token, int(error), EGLUtils::strerror(error));
65 }
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{
Jean-Baptiste Querucc8c35c2009-11-12 18:45:53 -080096 mNativeWindow = new FramebufferNativeWindow();
97 framebuffer_device_t const * fbDev = mNativeWindow->getDevice();
98
99 mOverlayEngine = NULL;
100 hw_module_t const* module;
101 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
Jean-Baptiste Querucc8c35c2009-11-12 18:45:53 -0800106 EGLint attribs[] = {
107 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
108 EGL_NONE, 0,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800109 EGL_NONE
110 };
Jean-Baptiste Querucc8c35c2009-11-12 18:45:53 -0800111
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;
Jean-Baptiste Querucc8c35c2009-11-12 18:45:53 -0800123 EGLint numConfigs=0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800124 EGLSurface surface;
125 EGLContext context;
Jean-Baptiste Querucc8c35c2009-11-12 18:45:53 -0800126 mFlags = CACHED_BUFFERS;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800127
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);
Jean-Baptiste Querucc8c35c2009-11-12 18:45:53 -0800134
135 EGLConfig config;
136 status_t err = EGLUtils::selectConfigForNativeWindow(
137 display, attribs, mNativeWindow.get(), &config);
138 LOGE_IF(err, "couldn't find an EGLConfig matching the screen format");
139
140 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);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800145
146 /*
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");
Jean-Baptiste Querucc8c35c2009-11-12 18:45:53 -0800159 LOGI("EGLSurface: %d-%d-%d-%d, config=%p", r, g, b, a, config);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800160
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800161
Jean-Baptiste Querucc8c35c2009-11-12 18:45:53 -0800162 if (mNativeWindow->isUpdateOnDemand()) {
163 mFlags |= PARTIAL_UPDATES;
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
Jean-Baptiste Querucc8c35c2009-11-12 18:45:53 -0800175 surface = eglCreateWindowSurface(display, config, mNativeWindow.get(), NULL);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800176
Jean-Baptiste Querucc8c35c2009-11-12 18:45:53 -0800177 if (mFlags & PARTIAL_UPDATES) {
178 // if we have partial updates, we definitely don't need to
179 // preserve the backbuffer, which may be costly.
180 eglSurfaceAttrib(display, surface,
181 EGL_SWAP_BEHAVIOR, EGL_BUFFER_DESTROYED);
182 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800183
184 if (eglQuerySurface(display, surface, EGL_SWAP_BEHAVIOR, &dummy) == EGL_TRUE) {
185 if (dummy == EGL_BUFFER_PRESERVED) {
186 mFlags |= BUFFER_PRESERVED;
187 }
188 }
Jean-Baptiste Querucc8c35c2009-11-12 18:45:53 -0800189
190 eglQuerySurface(display, surface, EGL_WIDTH, &mWidth);
191 eglQuerySurface(display, surface, EGL_HEIGHT, &mHeight);
192
193#ifdef EGL_ANDROID_swap_rectangle
194 if (strstr(egl_extensions, "EGL_ANDROID_swap_rectangle")) {
195 if (eglSetSwapRectangleANDROID(display, surface,
196 0, 0, mWidth, mHeight) == EGL_TRUE) {
197 // This could fail if this extension is not supported by this
198 // specific surface (of config)
199 mFlags |= SWAP_RECTANGLE;
200 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800201 }
Jean-Baptiste Querucc8c35c2009-11-12 18:45:53 -0800202 // when we have the choice between PARTIAL_UPDATES and SWAP_RECTANGLE
203 // choose PARTIAL_UPDATES, which should be more efficient
204 if (mFlags & PARTIAL_UPDATES)
205 mFlags &= ~SWAP_RECTANGLE;
206#endif
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800207
Jean-Baptiste Querucc8c35c2009-11-12 18:45:53 -0800208
209 LOGI("flags : %08x", mFlags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800210
Jean-Baptiste Querucc8c35c2009-11-12 18:45:53 -0800211 mDpiX = mNativeWindow->xdpi;
212 mDpiY = mNativeWindow->ydpi;
213 mRefreshRate = fbDev->fps;
214
Jean-Baptiste Queru4d3b5c12009-07-21 11:16:54 -0700215 /* Read density from build-specific ro.sf.lcd_density property
Jean-Baptiste Querucc8c35c2009-11-12 18:45:53 -0800216 * except if it is overridden by qemu.sf.lcd_density.
Jean-Baptiste Queru4d3b5c12009-07-21 11:16:54 -0700217 */
218 if (property_get("qemu.sf.lcd_density", property, NULL) <= 0) {
219 if (property_get("ro.sf.lcd_density", property, NULL) <= 0) {
220 LOGW("ro.sf.lcd_density not defined, using 160 dpi by default.");
221 strcpy(property, "160");
222 }
Jean-Baptiste Queru110b3142009-07-31 17:38:20 -0700223 } else {
224 /* for the emulator case, reset the dpi values too */
225 mDpiX = mDpiY = atoi(property);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800226 }
227 mDensity = atoi(property) * (1.0f/160.0f);
228
229
230 /*
231 * Create our OpenGL ES context
232 */
233
234 context = eglCreateContext(display, config, NULL, NULL);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800235
236 /*
237 * Gather OpenGL ES extensions
238 */
239
240 eglMakeCurrent(display, surface, surface, context);
241 const char* const gl_extensions = (const char*)glGetString(GL_EXTENSIONS);
Jean-Baptiste Querucc8c35c2009-11-12 18:45:53 -0800242 const char* const gl_renderer = (const char*)glGetString(GL_RENDERER);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800243 LOGI("OpenGL informations:");
244 LOGI("vendor : %s", glGetString(GL_VENDOR));
Jean-Baptiste Querucc8c35c2009-11-12 18:45:53 -0800245 LOGI("renderer : %s", gl_renderer);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800246 LOGI("version : %s", glGetString(GL_VERSION));
247 LOGI("extensions: %s", gl_extensions);
248
Jean-Baptiste Querucc8c35c2009-11-12 18:45:53 -0800249 if (strstr(gl_renderer, "PowerVR SGX 530")) {
250 LOGD("Assuming uncached graphics buffers.");
251 mFlags &= ~CACHED_BUFFERS;
252 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800253 if (strstr(gl_extensions, "GL_ARB_texture_non_power_of_two")) {
254 mFlags |= NPOT_EXTENSION;
255 }
256 if (strstr(gl_extensions, "GL_OES_draw_texture")) {
257 mFlags |= DRAW_TEXTURE_EXTENSION;
258 }
Jean-Baptiste Querucc8c35c2009-11-12 18:45:53 -0800259#ifdef EGL_ANDROID_image_native_buffer
260 if (strstr( gl_extensions, "GL_OES_EGL_image") &&
261 (strstr(egl_extensions, "EGL_KHR_image_base") ||
262 strstr(egl_extensions, "EGL_KHR_image")) &&
263 strstr(egl_extensions, "EGL_ANDROID_image_native_buffer")) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800264 mFlags |= DIRECT_TEXTURE;
265 }
Jean-Baptiste Querucc8c35c2009-11-12 18:45:53 -0800266#else
267#warning "EGL_ANDROID_image_native_buffer not supported"
268#endif
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800269
270 // Unbind the context from this thread
271 eglMakeCurrent(display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
272
273 mDisplay = display;
274 mConfig = config;
275 mSurface = surface;
276 mContext = context;
Jean-Baptiste Querucc8c35c2009-11-12 18:45:53 -0800277 mFormat = fbDev->format;
278 mPageFlipCount = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800279}
280
281/*
282 * Clean up. Throw out our local state.
283 *
284 * (It's entirely possible we'll never get here, since this is meant
285 * for real hardware, which doesn't restart.)
286 */
287
288void DisplayHardware::fini()
289{
290 eglMakeCurrent(mDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
291 eglTerminate(mDisplay);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800292 overlay_control_close(mOverlayEngine);
293}
294
295void DisplayHardware::releaseScreen() const
296{
297 DisplayHardwareBase::releaseScreen();
298}
299
300void DisplayHardware::acquireScreen() const
301{
302 DisplayHardwareBase::acquireScreen();
303}
304
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800305uint32_t DisplayHardware::getPageFlipCount() const {
Jean-Baptiste Querucc8c35c2009-11-12 18:45:53 -0800306 return mPageFlipCount;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800307}
308
Jean-Baptiste Querucc8c35c2009-11-12 18:45:53 -0800309status_t DisplayHardware::compositionComplete() const {
310 return mNativeWindow->compositionComplete();
311}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800312
313void DisplayHardware::flip(const Region& dirty) const
314{
315 checkGLErrors();
316
317 EGLDisplay dpy = mDisplay;
318 EGLSurface surface = mSurface;
319
Jean-Baptiste Querucc8c35c2009-11-12 18:45:53 -0800320#ifdef EGL_ANDROID_swap_rectangle
321 if (mFlags & SWAP_RECTANGLE) {
322 const Region newDirty(dirty.intersect(bounds()));
323 const Rect b(newDirty.getBounds());
324 eglSetSwapRectangleANDROID(dpy, surface,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800325 b.left, b.top, b.width(), b.height());
Jean-Baptiste Querucc8c35c2009-11-12 18:45:53 -0800326 }
327#endif
328
329 if (mFlags & PARTIAL_UPDATES) {
330 mNativeWindow->setUpdateRectangle(dirty.getBounds());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800331 }
Jean-Baptiste Querucc8c35c2009-11-12 18:45:53 -0800332
333 mPageFlipCount++;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800334 eglSwapBuffers(dpy, surface);
335 checkEGLErrors("eglSwapBuffers");
336
337 // for debugging
338 //glClearColor(1,0,0,0);
339 //glClear(GL_COLOR_BUFFER_BIT);
340}
341
342uint32_t DisplayHardware::getFlags() const
343{
344 return mFlags;
345}
346
347void DisplayHardware::makeCurrent() const
348{
349 eglMakeCurrent(mDisplay, mSurface, mSurface, mContext);
350}