blob: 64cff965b12fa2e905259ac22e615f8127a9a9e9 [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
Mathias Agopian076b1cc2009-04-10 14:24:30 -070039#include <hardware/gralloc.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080040
Mathias Agopian1f7bec62010-06-25 18:02:21 -070041#include "GLExtensions.h"
Mathias Agopiana350ff92010-08-10 17:14:02 -070042#include "HWComposer.h"
Mathias Agopian1f7bec62010-06-25 18:02:21 -070043
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080044using namespace android;
45
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080046
47static __attribute__((noinline))
48void checkGLErrors()
49{
Mathias Agopiancbb288b2009-09-07 16:32:45 -070050 do {
51 // there could be more than one error flag
52 GLenum error = glGetError();
53 if (error == GL_NO_ERROR)
54 break;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080055 LOGE("GL error 0x%04x", int(error));
Mathias Agopiancbb288b2009-09-07 16:32:45 -070056 } while(true);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080057}
58
59static __attribute__((noinline))
60void checkEGLErrors(const char* token)
61{
62 EGLint error = eglGetError();
Mathias Agopiancbb288b2009-09-07 16:32:45 -070063 if (error && error != EGL_SUCCESS) {
64 LOGE("%s: EGL error 0x%04x (%s)",
Mathias Agopian0928e312009-08-07 16:38:10 -070065 token, int(error), EGLUtils::strerror(error));
Mathias Agopiancbb288b2009-09-07 16:32:45 -070066 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080067}
68
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080069/*
70 * Initialize the display to the specified values.
71 *
72 */
73
74DisplayHardware::DisplayHardware(
75 const sp<SurfaceFlinger>& flinger,
76 uint32_t dpy)
Mathias Agopian1f7bec62010-06-25 18:02:21 -070077 : DisplayHardwareBase(flinger, dpy),
Mathias Agopiana350ff92010-08-10 17:14:02 -070078 mFlags(0), mHwc(0)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080079{
80 init(dpy);
81}
82
83DisplayHardware::~DisplayHardware()
84{
85 fini();
86}
87
88float DisplayHardware::getDpiX() const { return mDpiX; }
89float DisplayHardware::getDpiY() const { return mDpiY; }
90float DisplayHardware::getDensity() const { return mDensity; }
91float DisplayHardware::getRefreshRate() const { return mRefreshRate; }
92int DisplayHardware::getWidth() const { return mWidth; }
93int DisplayHardware::getHeight() const { return mHeight; }
94PixelFormat DisplayHardware::getFormat() const { return mFormat; }
Mathias Agopianca99fb82010-04-14 16:43:44 -070095uint32_t DisplayHardware::getMaxTextureSize() const { return mMaxTextureSize; }
96uint32_t DisplayHardware::getMaxViewportDims() const { return mMaxViewportDims; }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080097
98void DisplayHardware::init(uint32_t dpy)
99{
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700100 mNativeWindow = new FramebufferNativeWindow();
Mathias Agopian0928e312009-08-07 16:38:10 -0700101 framebuffer_device_t const * fbDev = mNativeWindow->getDevice();
Mathias Agopian1f7bec62010-06-25 18:02:21 -0700102 mDpiX = mNativeWindow->xdpi;
103 mDpiY = mNativeWindow->ydpi;
104 mRefreshRate = fbDev->fps;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700105
Mathias Agopian1f7bec62010-06-25 18:02:21 -0700106 EGLint w, h, dummy;
107 EGLint numConfigs=0;
108 EGLSurface surface;
109 EGLContext context;
110
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800111 // initialize EGL
Mathias Agopiana5b02e02009-09-04 18:49:03 -0700112 EGLint attribs[] = {
Mathias Agopian0928e312009-08-07 16:38:10 -0700113 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
Mathias Agopiana5b02e02009-09-04 18:49:03 -0700114 EGL_NONE, 0,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800115 EGL_NONE
116 };
Mathias Agopiana5b02e02009-09-04 18:49:03 -0700117
118 // debug: disable h/w rendering
119 char property[PROPERTY_VALUE_MAX];
120 if (property_get("debug.sf.hw", property, NULL) > 0) {
121 if (atoi(property) == 0) {
122 LOGW("H/W composition disabled");
123 attribs[2] = EGL_CONFIG_CAVEAT;
124 attribs[3] = EGL_SLOW_CONFIG;
125 }
126 }
127
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800128 // 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
Mathias Agopian1e16b132009-05-07 17:40:23 -0700146 if (mNativeWindow->isUpdateOnDemand()) {
Mathias Agopian95a666b2009-09-24 14:57:26 -0700147 mFlags |= PARTIAL_UPDATES;
Mathias Agopian1e16b132009-05-07 17:40:23 -0700148 }
149
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800150 if (eglGetConfigAttrib(display, config, EGL_CONFIG_CAVEAT, &dummy) == EGL_TRUE) {
151 if (dummy == EGL_SLOW_CONFIG)
152 mFlags |= SLOW_CONFIG;
153 }
154
155 /*
156 * Create our main surface
157 */
158
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700159 surface = eglCreateWindowSurface(display, config, mNativeWindow.get(), NULL);
Mathias Agopian1f7bec62010-06-25 18:02:21 -0700160 eglQuerySurface(display, surface, EGL_WIDTH, &mWidth);
161 eglQuerySurface(display, surface, EGL_HEIGHT, &mHeight);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800162
Mathias Agopian95a666b2009-09-24 14:57:26 -0700163 if (mFlags & PARTIAL_UPDATES) {
164 // if we have partial updates, we definitely don't need to
165 // preserve the backbuffer, which may be costly.
Mathias Agopian0928bee2009-09-16 20:15:42 -0700166 eglSurfaceAttrib(display, surface,
167 EGL_SWAP_BEHAVIOR, EGL_BUFFER_DESTROYED);
168 }
169
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800170 if (eglQuerySurface(display, surface, EGL_SWAP_BEHAVIOR, &dummy) == EGL_TRUE) {
171 if (dummy == EGL_BUFFER_PRESERVED) {
172 mFlags |= BUFFER_PRESERVED;
173 }
174 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800175
David 'Digit' Turnerae71acc2009-06-19 04:41:12 +0200176 /* Read density from build-specific ro.sf.lcd_density property
Mathias Agopian24e5f522009-08-12 21:18:15 -0700177 * except if it is overridden by qemu.sf.lcd_density.
David 'Digit' Turnerae71acc2009-06-19 04:41:12 +0200178 */
179 if (property_get("qemu.sf.lcd_density", property, NULL) <= 0) {
180 if (property_get("ro.sf.lcd_density", property, NULL) <= 0) {
181 LOGW("ro.sf.lcd_density not defined, using 160 dpi by default.");
182 strcpy(property, "160");
David 'Digit' Turner694e10b2009-06-18 04:30:32 +0200183 }
David 'Digit' Turner31469e12009-07-29 00:38:58 +0200184 } else {
185 /* for the emulator case, reset the dpi values too */
186 mDpiX = mDpiY = atoi(property);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800187 }
188 mDensity = atoi(property) * (1.0f/160.0f);
189
190
191 /*
192 * Create our OpenGL ES context
193 */
194
Mathias Agopian67226812010-10-11 17:54:43 -0700195
196 EGLint contextAttributes[] = {
197#ifdef EGL_IMG_context_priority
198#ifdef HAS_CONTEXT_PRIORITY
199#warning "using EGL_IMG_context_priority"
200 EGL_CONTEXT_PRIORITY_LEVEL_IMG, EGL_CONTEXT_PRIORITY_HIGH_IMG,
201#endif
202#endif
203 EGL_NONE, EGL_NONE
204 };
205 context = eglCreateContext(display, config, NULL, contextAttributes);
206
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800207 mDisplay = display;
208 mConfig = config;
209 mSurface = surface;
210 mContext = context;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700211 mFormat = fbDev->format;
212 mPageFlipCount = 0;
Mathias Agopian1f7bec62010-06-25 18:02:21 -0700213
214 /*
215 * Gather OpenGL ES extensions
216 */
217
218 eglMakeCurrent(display, surface, surface, context);
219
220 GLExtensions& extensions(GLExtensions::getInstance());
221 extensions.initWithGLStrings(
222 glGetString(GL_VENDOR),
223 glGetString(GL_RENDERER),
224 glGetString(GL_VERSION),
225 glGetString(GL_EXTENSIONS),
226 eglQueryString(display, EGL_VENDOR),
227 eglQueryString(display, EGL_VERSION),
228 eglQueryString(display, EGL_EXTENSIONS));
229
230 glGetIntegerv(GL_MAX_TEXTURE_SIZE, &mMaxTextureSize);
231 glGetIntegerv(GL_MAX_VIEWPORT_DIMS, &mMaxViewportDims);
232
233
234#ifdef EGL_ANDROID_swap_rectangle
235 if (extensions.hasExtension("EGL_ANDROID_swap_rectangle")) {
236 if (eglSetSwapRectangleANDROID(display, surface,
237 0, 0, mWidth, mHeight) == EGL_TRUE) {
238 // This could fail if this extension is not supported by this
239 // specific surface (of config)
240 mFlags |= SWAP_RECTANGLE;
241 }
242 }
243 // when we have the choice between PARTIAL_UPDATES and SWAP_RECTANGLE
244 // choose PARTIAL_UPDATES, which should be more efficient
245 if (mFlags & PARTIAL_UPDATES)
246 mFlags &= ~SWAP_RECTANGLE;
247#endif
248
249 LOGI("EGL informations:");
250 LOGI("# of configs : %d", numConfigs);
251 LOGI("vendor : %s", extensions.getEglVendor());
252 LOGI("version : %s", extensions.getEglVersion());
253 LOGI("extensions: %s", extensions.getEglExtension());
254 LOGI("Client API: %s", eglQueryString(display, EGL_CLIENT_APIS)?:"Not Supported");
255 LOGI("EGLSurface: %d-%d-%d-%d, config=%p", r, g, b, a, config);
256
257 LOGI("OpenGL informations:");
258 LOGI("vendor : %s", extensions.getVendor());
259 LOGI("renderer : %s", extensions.getRenderer());
260 LOGI("version : %s", extensions.getVersion());
261 LOGI("extensions: %s", extensions.getExtension());
262 LOGI("GL_MAX_TEXTURE_SIZE = %d", mMaxTextureSize);
263 LOGI("GL_MAX_VIEWPORT_DIMS = %d", mMaxViewportDims);
264 LOGI("flags = %08x", mFlags);
265
266 // Unbind the context from this thread
267 eglMakeCurrent(display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
Mathias Agopiana350ff92010-08-10 17:14:02 -0700268
269
270 // initialize the H/W composer
271 mHwc = new HWComposer();
272 if (mHwc->initCheck() == NO_ERROR) {
273 mHwc->setFrameBuffer(mDisplay, mSurface);
274 }
275}
276
277HWComposer& DisplayHardware::getHwComposer() const {
278 return *mHwc;
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}
293
294void DisplayHardware::releaseScreen() const
295{
296 DisplayHardwareBase::releaseScreen();
Antti Hatalaf5f27122010-09-09 02:33:05 -0700297 if (mHwc->initCheck() == NO_ERROR) {
298 mHwc->release();
299 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800300}
301
302void DisplayHardware::acquireScreen() const
303{
304 DisplayHardwareBase::acquireScreen();
305}
306
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800307uint32_t DisplayHardware::getPageFlipCount() const {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700308 return mPageFlipCount;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800309}
310
Mathias Agopian74faca22009-09-17 16:18:16 -0700311status_t DisplayHardware::compositionComplete() const {
312 return mNativeWindow->compositionComplete();
313}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800314
Mathias Agopian35b48d12010-09-13 22:57:58 -0700315int DisplayHardware::getCurrentBufferIndex() const {
316 return mNativeWindow->getCurrentBufferIndex();
317}
318
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800319void DisplayHardware::flip(const Region& dirty) const
320{
321 checkGLErrors();
322
323 EGLDisplay dpy = mDisplay;
324 EGLSurface surface = mSurface;
325
Mathias Agopian5e78e092009-06-11 17:19:54 -0700326#ifdef EGL_ANDROID_swap_rectangle
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700327 if (mFlags & SWAP_RECTANGLE) {
Mathias Agopianb8a55602009-06-26 19:06:36 -0700328 const Region newDirty(dirty.intersect(bounds()));
329 const Rect b(newDirty.getBounds());
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700330 eglSetSwapRectangleANDROID(dpy, surface,
331 b.left, b.top, b.width(), b.height());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800332 }
Mathias Agopian5e78e092009-06-11 17:19:54 -0700333#endif
334
Mathias Agopian95a666b2009-09-24 14:57:26 -0700335 if (mFlags & PARTIAL_UPDATES) {
Mathias Agopian29d06ac2009-06-29 18:49:56 -0700336 mNativeWindow->setUpdateRectangle(dirty.getBounds());
Mathias Agopian1e16b132009-05-07 17:40:23 -0700337 }
338
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700339 mPageFlipCount++;
Mathias Agopiana350ff92010-08-10 17:14:02 -0700340
341 if (mHwc->initCheck() == NO_ERROR) {
342 mHwc->commit();
343 } else {
344 eglSwapBuffers(dpy, surface);
345 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800346 checkEGLErrors("eglSwapBuffers");
347
348 // for debugging
349 //glClearColor(1,0,0,0);
350 //glClear(GL_COLOR_BUFFER_BIT);
351}
352
353uint32_t DisplayHardware::getFlags() const
354{
355 return mFlags;
356}
357
358void DisplayHardware::makeCurrent() const
359{
360 eglMakeCurrent(mDisplay, mSurface, mSurface, mContext);
361}
Erik Gilling1d21a9c2010-12-01 16:38:01 -0800362
363void DisplayHardware::dump(String8& res) const
364{
365 mNativeWindow->dump(res);
366}