1 /*
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
17 #include <stdlib.h>
18 #include <stdio.h>
19 #include <string.h>
20 #include <math.h>
21
22 #include <cutils/properties.h>
23
24 #include <utils/RefBase.h>
25 #include <utils/Log.h>
26
27 #include <ui/PixelFormat.h>
28 #include <ui/FramebufferNativeWindow.h>
29 #include <ui/EGLUtils.h>
30
31 #include <GLES/gl.h>
32 #include <EGL/egl.h>
33 #include <EGL/eglext.h>
34
35 #include <pixelflinger/pixelflinger.h>
36
37 #include "DisplayHardware/DisplayHardware.h"
38
39 #include <hardware/copybit.h>
40 #include <hardware/overlay.h>
41 #include <hardware/gralloc.h>
42
43 using namespace android;
44
45
46 static __attribute__((noinline))
checkGLErrors()47 void checkGLErrors()
48 {
49 do {
50 // there could be more than one error flag
51 GLenum error = glGetError();
52 if (error == GL_NO_ERROR)
53 break;
54 LOGE("GL error 0x%04x", int(error));
55 } while(true);
56 }
57
58 static __attribute__((noinline))
checkEGLErrors(const char * token)59 void checkEGLErrors(const char* token)
60 {
61 EGLint error = eglGetError();
62 if (error && error != EGL_SUCCESS) {
63 LOGE("%s: EGL error 0x%04x (%s)",
64 token, int(error), EGLUtils::strerror(error));
65 }
66 }
67
68 /*
69 * Initialize the display to the specified values.
70 *
71 */
72
DisplayHardware(const sp<SurfaceFlinger> & flinger,uint32_t dpy)73 DisplayHardware::DisplayHardware(
74 const sp<SurfaceFlinger>& flinger,
75 uint32_t dpy)
76 : DisplayHardwareBase(flinger, dpy)
77 {
78 init(dpy);
79 }
80
~DisplayHardware()81 DisplayHardware::~DisplayHardware()
82 {
83 fini();
84 }
85
getDpiX() const86 float DisplayHardware::getDpiX() const { return mDpiX; }
getDpiY() const87 float DisplayHardware::getDpiY() const { return mDpiY; }
getDensity() const88 float DisplayHardware::getDensity() const { return mDensity; }
getRefreshRate() const89 float DisplayHardware::getRefreshRate() const { return mRefreshRate; }
getWidth() const90 int DisplayHardware::getWidth() const { return mWidth; }
getHeight() const91 int DisplayHardware::getHeight() const { return mHeight; }
getFormat() const92 PixelFormat DisplayHardware::getFormat() const { return mFormat; }
93
init(uint32_t dpy)94 void DisplayHardware::init(uint32_t dpy)
95 {
96 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
105 // initialize EGL
106 EGLint attribs[] = {
107 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
108 EGL_NONE, 0,
109 EGL_NONE
110 };
111
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
122 EGLint w, h, dummy;
123 EGLint numConfigs=0;
124 EGLSurface surface;
125 EGLContext context;
126 mFlags = CACHED_BUFFERS;
127
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);
134
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);
145
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");
159 LOGI("EGLSurface: %d-%d-%d-%d, config=%p", r, g, b, a, config);
160
161
162 if (mNativeWindow->isUpdateOnDemand()) {
163 mFlags |= PARTIAL_UPDATES;
164 }
165
166 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
175 surface = eglCreateWindowSurface(display, config, mNativeWindow.get(), NULL);
176
177 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 }
183
184 if (eglQuerySurface(display, surface, EGL_SWAP_BEHAVIOR, &dummy) == EGL_TRUE) {
185 if (dummy == EGL_BUFFER_PRESERVED) {
186 mFlags |= BUFFER_PRESERVED;
187 }
188 }
189
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 }
201 }
202 // 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
207
208
209 LOGI("flags : %08x", mFlags);
210
211 mDpiX = mNativeWindow->xdpi;
212 mDpiY = mNativeWindow->ydpi;
213 mRefreshRate = fbDev->fps;
214
215 /* Read density from build-specific ro.sf.lcd_density property
216 * except if it is overridden by qemu.sf.lcd_density.
217 */
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 }
223 } else {
224 /* for the emulator case, reset the dpi values too */
225 mDpiX = mDpiY = atoi(property);
226 }
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);
235
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);
242 const char* const gl_renderer = (const char*)glGetString(GL_RENDERER);
243 LOGI("OpenGL informations:");
244 LOGI("vendor : %s", glGetString(GL_VENDOR));
245 LOGI("renderer : %s", gl_renderer);
246 LOGI("version : %s", glGetString(GL_VERSION));
247 LOGI("extensions: %s", gl_extensions);
248
249 if (strstr(gl_renderer, "PowerVR SGX 530") ||
250 strstr(gl_renderer, "Adreno")) {
251 LOGD("Assuming uncached graphics buffers.");
252 mFlags &= ~CACHED_BUFFERS;
253 }
254
255 if (strstr(gl_extensions, "GL_ARB_texture_non_power_of_two")) {
256 mFlags |= NPOT_EXTENSION;
257 }
258 if (strstr(gl_extensions, "GL_OES_draw_texture")) {
259 mFlags |= DRAW_TEXTURE_EXTENSION;
260 }
261 #ifdef EGL_ANDROID_image_native_buffer
262 if (strstr( gl_extensions, "GL_OES_EGL_image") &&
263 (strstr(egl_extensions, "EGL_KHR_image_base") ||
264 strstr(egl_extensions, "EGL_KHR_image")) &&
265 strstr(egl_extensions, "EGL_ANDROID_image_native_buffer")) {
266 mFlags |= DIRECT_TEXTURE;
267 }
268 #else
269 #warning "EGL_ANDROID_image_native_buffer not supported"
270 #endif
271
272 // Unbind the context from this thread
273 eglMakeCurrent(display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
274
275 mDisplay = display;
276 mConfig = config;
277 mSurface = surface;
278 mContext = context;
279 mFormat = fbDev->format;
280 mPageFlipCount = 0;
281 }
282
283 /*
284 * Clean up. Throw out our local state.
285 *
286 * (It's entirely possible we'll never get here, since this is meant
287 * for real hardware, which doesn't restart.)
288 */
289
fini()290 void DisplayHardware::fini()
291 {
292 eglMakeCurrent(mDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
293 eglTerminate(mDisplay);
294 overlay_control_close(mOverlayEngine);
295 }
296
releaseScreen() const297 void DisplayHardware::releaseScreen() const
298 {
299 DisplayHardwareBase::releaseScreen();
300 }
301
acquireScreen() const302 void DisplayHardware::acquireScreen() const
303 {
304 DisplayHardwareBase::acquireScreen();
305 }
306
getPageFlipCount() const307 uint32_t DisplayHardware::getPageFlipCount() const {
308 return mPageFlipCount;
309 }
310
compositionComplete() const311 status_t DisplayHardware::compositionComplete() const {
312 return mNativeWindow->compositionComplete();
313 }
314
flip(const Region & dirty) const315 void DisplayHardware::flip(const Region& dirty) const
316 {
317 checkGLErrors();
318
319 EGLDisplay dpy = mDisplay;
320 EGLSurface surface = mSurface;
321
322 #ifdef EGL_ANDROID_swap_rectangle
323 if (mFlags & SWAP_RECTANGLE) {
324 const Region newDirty(dirty.intersect(bounds()));
325 const Rect b(newDirty.getBounds());
326 eglSetSwapRectangleANDROID(dpy, surface,
327 b.left, b.top, b.width(), b.height());
328 }
329 #endif
330
331 if (mFlags & PARTIAL_UPDATES) {
332 mNativeWindow->setUpdateRectangle(dirty.getBounds());
333 }
334
335 mPageFlipCount++;
336 eglSwapBuffers(dpy, surface);
337 checkEGLErrors("eglSwapBuffers");
338
339 // for debugging
340 //glClearColor(1,0,0,0);
341 //glClear(GL_COLOR_BUFFER_BIT);
342 }
343
getFlags() const344 uint32_t DisplayHardware::getFlags() const
345 {
346 return mFlags;
347 }
348
makeCurrent() const349 void DisplayHardware::makeCurrent() const
350 {
351 eglMakeCurrent(mDisplay, mSurface, mSurface, mContext);
352 }
353