blob: 7bf3e0ae0957970f8a5da7b80bc8a6a137f4b26e [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; }
Mathias Agopian3d64e732011-04-18 15:59:24 -070096
97uint32_t DisplayHardware::getMaxViewportDims() const {
98 return mMaxViewportDims[0] < mMaxViewportDims[1] ?
99 mMaxViewportDims[0] : mMaxViewportDims[1];
100}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800101
Mathias Agopian61630912011-07-06 16:35:30 -0700102static status_t selectConfigForPixelFormat(
103 EGLDisplay dpy,
104 EGLint const* attrs,
105 PixelFormat format,
106 EGLConfig* outConfig)
107{
108 EGLConfig config = NULL;
109 EGLint numConfigs = -1, n=0;
110 eglGetConfigs(dpy, NULL, 0, &numConfigs);
111 EGLConfig* const configs = new EGLConfig[numConfigs];
112 eglChooseConfig(dpy, attrs, configs, numConfigs, &n);
113 for (int i=0 ; i<n ; i++) {
114 EGLint nativeVisualId = 0;
115 eglGetConfigAttrib(dpy, configs[i], EGL_NATIVE_VISUAL_ID, &nativeVisualId);
116 if (nativeVisualId>0 && format == nativeVisualId) {
117 *outConfig = configs[i];
118 delete [] configs;
119 return NO_ERROR;
120 }
121 }
122 delete [] configs;
123 return NAME_NOT_FOUND;
124}
125
126
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800127void DisplayHardware::init(uint32_t dpy)
128{
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700129 mNativeWindow = new FramebufferNativeWindow();
Mathias Agopian0928e312009-08-07 16:38:10 -0700130 framebuffer_device_t const * fbDev = mNativeWindow->getDevice();
Mathias Agopian1f339ff2011-07-01 17:08:43 -0700131 if (!fbDev) {
132 LOGE("Display subsystem failed to initialize. check logs. exiting...");
133 exit(0);
134 }
135
Mathias Agopian61630912011-07-06 16:35:30 -0700136 int format;
137 ANativeWindow const * const window = mNativeWindow.get();
138 window->query(window, NATIVE_WINDOW_FORMAT, &format);
Mathias Agopian1f7bec62010-06-25 18:02:21 -0700139 mDpiX = mNativeWindow->xdpi;
140 mDpiY = mNativeWindow->ydpi;
141 mRefreshRate = fbDev->fps;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700142
Mathias Agopian1f7bec62010-06-25 18:02:21 -0700143 EGLint w, h, dummy;
144 EGLint numConfigs=0;
145 EGLSurface surface;
146 EGLContext context;
Mathias Agopian61630912011-07-06 16:35:30 -0700147 EGLBoolean result;
148 status_t err;
Mathias Agopian1f7bec62010-06-25 18:02:21 -0700149
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800150 // initialize EGL
Mathias Agopiana5b02e02009-09-04 18:49:03 -0700151 EGLint attribs[] = {
Mathias Agopian61630912011-07-06 16:35:30 -0700152 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
153 EGL_NONE, 0,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800154 EGL_NONE
155 };
Mathias Agopiana5b02e02009-09-04 18:49:03 -0700156
157 // debug: disable h/w rendering
158 char property[PROPERTY_VALUE_MAX];
159 if (property_get("debug.sf.hw", property, NULL) > 0) {
160 if (atoi(property) == 0) {
161 LOGW("H/W composition disabled");
162 attribs[2] = EGL_CONFIG_CAVEAT;
163 attribs[3] = EGL_SLOW_CONFIG;
164 }
165 }
166
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800167 // TODO: all the extensions below should be queried through
168 // eglGetProcAddress().
169
170 EGLDisplay display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
171 eglInitialize(display, NULL, NULL);
172 eglGetConfigs(display, NULL, 0, &numConfigs);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700173
Mathias Agopian61630912011-07-06 16:35:30 -0700174 EGLConfig config = NULL;
175 err = selectConfigForPixelFormat(display, attribs, format, &config);
Mathias Agopian6cf50a72009-08-06 16:05:39 -0700176 LOGE_IF(err, "couldn't find an EGLConfig matching the screen format");
177
Mathias Agopian0928e312009-08-07 16:38:10 -0700178 EGLint r,g,b,a;
179 eglGetConfigAttrib(display, config, EGL_RED_SIZE, &r);
180 eglGetConfigAttrib(display, config, EGL_GREEN_SIZE, &g);
181 eglGetConfigAttrib(display, config, EGL_BLUE_SIZE, &b);
182 eglGetConfigAttrib(display, config, EGL_ALPHA_SIZE, &a);
183
Mathias Agopian1e16b132009-05-07 17:40:23 -0700184 if (mNativeWindow->isUpdateOnDemand()) {
Mathias Agopian95a666b2009-09-24 14:57:26 -0700185 mFlags |= PARTIAL_UPDATES;
Mathias Agopian1e16b132009-05-07 17:40:23 -0700186 }
187
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800188 if (eglGetConfigAttrib(display, config, EGL_CONFIG_CAVEAT, &dummy) == EGL_TRUE) {
189 if (dummy == EGL_SLOW_CONFIG)
190 mFlags |= SLOW_CONFIG;
191 }
192
193 /*
194 * Create our main surface
195 */
196
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700197 surface = eglCreateWindowSurface(display, config, mNativeWindow.get(), NULL);
Mathias Agopian1f7bec62010-06-25 18:02:21 -0700198 eglQuerySurface(display, surface, EGL_WIDTH, &mWidth);
199 eglQuerySurface(display, surface, EGL_HEIGHT, &mHeight);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800200
Mathias Agopian95a666b2009-09-24 14:57:26 -0700201 if (mFlags & PARTIAL_UPDATES) {
202 // if we have partial updates, we definitely don't need to
203 // preserve the backbuffer, which may be costly.
Mathias Agopian0928bee2009-09-16 20:15:42 -0700204 eglSurfaceAttrib(display, surface,
205 EGL_SWAP_BEHAVIOR, EGL_BUFFER_DESTROYED);
206 }
207
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800208 if (eglQuerySurface(display, surface, EGL_SWAP_BEHAVIOR, &dummy) == EGL_TRUE) {
209 if (dummy == EGL_BUFFER_PRESERVED) {
210 mFlags |= BUFFER_PRESERVED;
211 }
212 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800213
David 'Digit' Turnerae71acc2009-06-19 04:41:12 +0200214 /* Read density from build-specific ro.sf.lcd_density property
Mathias Agopian24e5f522009-08-12 21:18:15 -0700215 * except if it is overridden by qemu.sf.lcd_density.
David 'Digit' Turnerae71acc2009-06-19 04:41:12 +0200216 */
217 if (property_get("qemu.sf.lcd_density", property, NULL) <= 0) {
218 if (property_get("ro.sf.lcd_density", property, NULL) <= 0) {
219 LOGW("ro.sf.lcd_density not defined, using 160 dpi by default.");
220 strcpy(property, "160");
David 'Digit' Turner694e10b2009-06-18 04:30:32 +0200221 }
David 'Digit' Turner31469e12009-07-29 00:38:58 +0200222 } else {
223 /* for the emulator case, reset the dpi values too */
224 mDpiX = mDpiY = atoi(property);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800225 }
226 mDensity = atoi(property) * (1.0f/160.0f);
227
228
229 /*
230 * Create our OpenGL ES context
231 */
232
Mathias Agopian67226812010-10-11 17:54:43 -0700233
234 EGLint contextAttributes[] = {
235#ifdef EGL_IMG_context_priority
236#ifdef HAS_CONTEXT_PRIORITY
237#warning "using EGL_IMG_context_priority"
238 EGL_CONTEXT_PRIORITY_LEVEL_IMG, EGL_CONTEXT_PRIORITY_HIGH_IMG,
239#endif
240#endif
241 EGL_NONE, EGL_NONE
242 };
243 context = eglCreateContext(display, config, NULL, contextAttributes);
244
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800245 mDisplay = display;
246 mConfig = config;
247 mSurface = surface;
248 mContext = context;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700249 mFormat = fbDev->format;
250 mPageFlipCount = 0;
Mathias Agopian1f7bec62010-06-25 18:02:21 -0700251
252 /*
253 * Gather OpenGL ES extensions
254 */
255
Mathias Agopian61630912011-07-06 16:35:30 -0700256 result = eglMakeCurrent(display, surface, surface, context);
257 if (!result) {
258 LOGE("Couldn't create a working GLES context. check logs. exiting...");
259 exit(0);
260 }
Mathias Agopian1f7bec62010-06-25 18:02:21 -0700261
262 GLExtensions& extensions(GLExtensions::getInstance());
263 extensions.initWithGLStrings(
264 glGetString(GL_VENDOR),
265 glGetString(GL_RENDERER),
266 glGetString(GL_VERSION),
267 glGetString(GL_EXTENSIONS),
268 eglQueryString(display, EGL_VENDOR),
269 eglQueryString(display, EGL_VERSION),
270 eglQueryString(display, EGL_EXTENSIONS));
271
272 glGetIntegerv(GL_MAX_TEXTURE_SIZE, &mMaxTextureSize);
Mathias Agopian3d64e732011-04-18 15:59:24 -0700273 glGetIntegerv(GL_MAX_VIEWPORT_DIMS, mMaxViewportDims);
Mathias Agopian1f7bec62010-06-25 18:02:21 -0700274
275
276#ifdef EGL_ANDROID_swap_rectangle
277 if (extensions.hasExtension("EGL_ANDROID_swap_rectangle")) {
278 if (eglSetSwapRectangleANDROID(display, surface,
279 0, 0, mWidth, mHeight) == EGL_TRUE) {
280 // This could fail if this extension is not supported by this
281 // specific surface (of config)
282 mFlags |= SWAP_RECTANGLE;
283 }
284 }
285 // when we have the choice between PARTIAL_UPDATES and SWAP_RECTANGLE
286 // choose PARTIAL_UPDATES, which should be more efficient
287 if (mFlags & PARTIAL_UPDATES)
288 mFlags &= ~SWAP_RECTANGLE;
289#endif
290
291 LOGI("EGL informations:");
292 LOGI("# of configs : %d", numConfigs);
293 LOGI("vendor : %s", extensions.getEglVendor());
294 LOGI("version : %s", extensions.getEglVersion());
295 LOGI("extensions: %s", extensions.getEglExtension());
296 LOGI("Client API: %s", eglQueryString(display, EGL_CLIENT_APIS)?:"Not Supported");
297 LOGI("EGLSurface: %d-%d-%d-%d, config=%p", r, g, b, a, config);
298
299 LOGI("OpenGL informations:");
300 LOGI("vendor : %s", extensions.getVendor());
301 LOGI("renderer : %s", extensions.getRenderer());
302 LOGI("version : %s", extensions.getVersion());
303 LOGI("extensions: %s", extensions.getExtension());
304 LOGI("GL_MAX_TEXTURE_SIZE = %d", mMaxTextureSize);
Mathias Agopian3d64e732011-04-18 15:59:24 -0700305 LOGI("GL_MAX_VIEWPORT_DIMS = %d x %d", mMaxViewportDims[0], mMaxViewportDims[1]);
Mathias Agopian1f7bec62010-06-25 18:02:21 -0700306 LOGI("flags = %08x", mFlags);
307
308 // Unbind the context from this thread
309 eglMakeCurrent(display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
Mathias Agopiana350ff92010-08-10 17:14:02 -0700310
311
312 // initialize the H/W composer
313 mHwc = new HWComposer();
314 if (mHwc->initCheck() == NO_ERROR) {
315 mHwc->setFrameBuffer(mDisplay, mSurface);
316 }
317}
318
319HWComposer& DisplayHardware::getHwComposer() const {
320 return *mHwc;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800321}
322
323/*
324 * Clean up. Throw out our local state.
325 *
326 * (It's entirely possible we'll never get here, since this is meant
327 * for real hardware, which doesn't restart.)
328 */
329
330void DisplayHardware::fini()
331{
332 eglMakeCurrent(mDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
333 eglTerminate(mDisplay);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800334}
335
336void DisplayHardware::releaseScreen() const
337{
338 DisplayHardwareBase::releaseScreen();
Antti Hatalaf5f27122010-09-09 02:33:05 -0700339 if (mHwc->initCheck() == NO_ERROR) {
340 mHwc->release();
341 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800342}
343
344void DisplayHardware::acquireScreen() const
345{
346 DisplayHardwareBase::acquireScreen();
347}
348
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800349uint32_t DisplayHardware::getPageFlipCount() const {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700350 return mPageFlipCount;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800351}
352
Mathias Agopian74faca22009-09-17 16:18:16 -0700353status_t DisplayHardware::compositionComplete() const {
354 return mNativeWindow->compositionComplete();
355}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800356
Mathias Agopian35b48d12010-09-13 22:57:58 -0700357int DisplayHardware::getCurrentBufferIndex() const {
358 return mNativeWindow->getCurrentBufferIndex();
359}
360
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800361void DisplayHardware::flip(const Region& dirty) const
362{
363 checkGLErrors();
364
365 EGLDisplay dpy = mDisplay;
366 EGLSurface surface = mSurface;
367
Mathias Agopian5e78e092009-06-11 17:19:54 -0700368#ifdef EGL_ANDROID_swap_rectangle
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700369 if (mFlags & SWAP_RECTANGLE) {
Mathias Agopianb8a55602009-06-26 19:06:36 -0700370 const Region newDirty(dirty.intersect(bounds()));
371 const Rect b(newDirty.getBounds());
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700372 eglSetSwapRectangleANDROID(dpy, surface,
373 b.left, b.top, b.width(), b.height());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800374 }
Mathias Agopian5e78e092009-06-11 17:19:54 -0700375#endif
376
Mathias Agopian95a666b2009-09-24 14:57:26 -0700377 if (mFlags & PARTIAL_UPDATES) {
Mathias Agopian29d06ac2009-06-29 18:49:56 -0700378 mNativeWindow->setUpdateRectangle(dirty.getBounds());
Mathias Agopian1e16b132009-05-07 17:40:23 -0700379 }
380
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700381 mPageFlipCount++;
Mathias Agopiana350ff92010-08-10 17:14:02 -0700382
383 if (mHwc->initCheck() == NO_ERROR) {
384 mHwc->commit();
385 } else {
386 eglSwapBuffers(dpy, surface);
387 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800388 checkEGLErrors("eglSwapBuffers");
389
390 // for debugging
391 //glClearColor(1,0,0,0);
392 //glClear(GL_COLOR_BUFFER_BIT);
393}
394
395uint32_t DisplayHardware::getFlags() const
396{
397 return mFlags;
398}
399
400void DisplayHardware::makeCurrent() const
401{
402 eglMakeCurrent(mDisplay, mSurface, mSurface, mContext);
403}
Erik Gilling1d21a9c2010-12-01 16:38:01 -0800404
405void DisplayHardware::dump(String8& res) const
406{
407 mNativeWindow->dump(res);
408}