• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/gralloc.h>
40 
41 #include "GLExtensions.h"
42 #include "HWComposer.h"
43 #include "SurfaceFlinger.h"
44 
45 using namespace android;
46 
47 
48 static __attribute__((noinline))
checkGLErrors()49 void checkGLErrors()
50 {
51     do {
52         // there could be more than one error flag
53         GLenum error = glGetError();
54         if (error == GL_NO_ERROR)
55             break;
56         LOGE("GL error 0x%04x", int(error));
57     } while(true);
58 }
59 
60 static __attribute__((noinline))
checkEGLErrors(const char * token)61 void checkEGLErrors(const char* token)
62 {
63     EGLint error = eglGetError();
64     if (error && error != EGL_SUCCESS) {
65         LOGE("%s: EGL error 0x%04x (%s)",
66                 token, int(error), EGLUtils::strerror(error));
67     }
68 }
69 
70 /*
71  * Initialize the display to the specified values.
72  *
73  */
74 
DisplayHardware(const sp<SurfaceFlinger> & flinger,uint32_t dpy)75 DisplayHardware::DisplayHardware(
76         const sp<SurfaceFlinger>& flinger,
77         uint32_t dpy)
78     : DisplayHardwareBase(flinger, dpy),
79       mFlinger(flinger), mFlags(0), mHwc(0)
80 {
81     init(dpy);
82 }
83 
~DisplayHardware()84 DisplayHardware::~DisplayHardware()
85 {
86     fini();
87 }
88 
getDpiX() const89 float DisplayHardware::getDpiX() const          { return mDpiX; }
getDpiY() const90 float DisplayHardware::getDpiY() const          { return mDpiY; }
getDensity() const91 float DisplayHardware::getDensity() const       { return mDensity; }
getRefreshRate() const92 float DisplayHardware::getRefreshRate() const   { return mRefreshRate; }
getWidth() const93 int DisplayHardware::getWidth() const           { return mWidth; }
getHeight() const94 int DisplayHardware::getHeight() const          { return mHeight; }
getFormat() const95 PixelFormat DisplayHardware::getFormat() const  { return mFormat; }
getMaxTextureSize() const96 uint32_t DisplayHardware::getMaxTextureSize() const { return mMaxTextureSize; }
97 
getMaxViewportDims() const98 uint32_t DisplayHardware::getMaxViewportDims() const {
99     return mMaxViewportDims[0] < mMaxViewportDims[1] ?
100             mMaxViewportDims[0] : mMaxViewportDims[1];
101 }
102 
selectConfigForPixelFormat(EGLDisplay dpy,EGLint const * attrs,PixelFormat format,EGLConfig * outConfig)103 static status_t selectConfigForPixelFormat(
104         EGLDisplay dpy,
105         EGLint const* attrs,
106         PixelFormat format,
107         EGLConfig* outConfig)
108 {
109     EGLConfig config = NULL;
110     EGLint numConfigs = -1, n=0;
111     eglGetConfigs(dpy, NULL, 0, &numConfigs);
112     EGLConfig* const configs = new EGLConfig[numConfigs];
113     eglChooseConfig(dpy, attrs, configs, numConfigs, &n);
114     for (int i=0 ; i<n ; i++) {
115         EGLint nativeVisualId = 0;
116         eglGetConfigAttrib(dpy, configs[i], EGL_NATIVE_VISUAL_ID, &nativeVisualId);
117         if (nativeVisualId>0 && format == nativeVisualId) {
118             *outConfig = configs[i];
119             delete [] configs;
120             return NO_ERROR;
121         }
122     }
123     delete [] configs;
124     return NAME_NOT_FOUND;
125 }
126 
127 
init(uint32_t dpy)128 void DisplayHardware::init(uint32_t dpy)
129 {
130     mNativeWindow = new FramebufferNativeWindow();
131     framebuffer_device_t const * fbDev = mNativeWindow->getDevice();
132     if (!fbDev) {
133         LOGE("Display subsystem failed to initialize. check logs. exiting...");
134         exit(0);
135     }
136 
137     int format;
138     ANativeWindow const * const window = mNativeWindow.get();
139     window->query(window, NATIVE_WINDOW_FORMAT, &format);
140     mDpiX = mNativeWindow->xdpi;
141     mDpiY = mNativeWindow->ydpi;
142     mRefreshRate = fbDev->fps;
143 
144     EGLint w, h, dummy;
145     EGLint numConfigs=0;
146     EGLSurface surface;
147     EGLContext context;
148     EGLBoolean result;
149     status_t err;
150 
151     // initialize EGL
152     EGLint attribs[] = {
153             EGL_SURFACE_TYPE,       EGL_WINDOW_BIT,
154             EGL_NONE,               0,
155             EGL_NONE
156     };
157 
158     // debug: disable h/w rendering
159     char property[PROPERTY_VALUE_MAX];
160     if (property_get("debug.sf.hw", property, NULL) > 0) {
161         if (atoi(property) == 0) {
162             LOGW("H/W composition disabled");
163             attribs[2] = EGL_CONFIG_CAVEAT;
164             attribs[3] = EGL_SLOW_CONFIG;
165         }
166     }
167 
168     // TODO: all the extensions below should be queried through
169     // eglGetProcAddress().
170 
171     EGLDisplay display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
172     eglInitialize(display, NULL, NULL);
173     eglGetConfigs(display, NULL, 0, &numConfigs);
174 
175     EGLConfig config = NULL;
176     err = selectConfigForPixelFormat(display, attribs, format, &config);
177     LOGE_IF(err, "couldn't find an EGLConfig matching the screen format");
178 
179     EGLint r,g,b,a;
180     eglGetConfigAttrib(display, config, EGL_RED_SIZE,   &r);
181     eglGetConfigAttrib(display, config, EGL_GREEN_SIZE, &g);
182     eglGetConfigAttrib(display, config, EGL_BLUE_SIZE,  &b);
183     eglGetConfigAttrib(display, config, EGL_ALPHA_SIZE, &a);
184 
185     if (mNativeWindow->isUpdateOnDemand()) {
186         mFlags |= PARTIAL_UPDATES;
187     }
188 
189     if (eglGetConfigAttrib(display, config, EGL_CONFIG_CAVEAT, &dummy) == EGL_TRUE) {
190         if (dummy == EGL_SLOW_CONFIG)
191             mFlags |= SLOW_CONFIG;
192     }
193 
194     /*
195      * Create our main surface
196      */
197 
198     surface = eglCreateWindowSurface(display, config, mNativeWindow.get(), NULL);
199     eglQuerySurface(display, surface, EGL_WIDTH,  &mWidth);
200     eglQuerySurface(display, surface, EGL_HEIGHT, &mHeight);
201 
202     if (mFlags & PARTIAL_UPDATES) {
203         // if we have partial updates, we definitely don't need to
204         // preserve the backbuffer, which may be costly.
205         eglSurfaceAttrib(display, surface,
206                 EGL_SWAP_BEHAVIOR, EGL_BUFFER_DESTROYED);
207     }
208 
209     if (eglQuerySurface(display, surface, EGL_SWAP_BEHAVIOR, &dummy) == EGL_TRUE) {
210         if (dummy == EGL_BUFFER_PRESERVED) {
211             mFlags |= BUFFER_PRESERVED;
212         }
213     }
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 
235     EGLint contextAttributes[] = {
236 #ifdef EGL_IMG_context_priority
237 #ifdef HAS_CONTEXT_PRIORITY
238 #warning "using EGL_IMG_context_priority"
239         EGL_CONTEXT_PRIORITY_LEVEL_IMG, EGL_CONTEXT_PRIORITY_HIGH_IMG,
240 #endif
241 #endif
242         EGL_NONE, EGL_NONE
243     };
244     context = eglCreateContext(display, config, NULL, contextAttributes);
245 
246     mDisplay = display;
247     mConfig  = config;
248     mSurface = surface;
249     mContext = context;
250     mFormat  = fbDev->format;
251     mPageFlipCount = 0;
252 
253     /*
254      * Gather OpenGL ES extensions
255      */
256 
257     result = eglMakeCurrent(display, surface, surface, context);
258     if (!result) {
259         LOGE("Couldn't create a working GLES context. check logs. exiting...");
260         exit(0);
261     }
262 
263     GLExtensions& extensions(GLExtensions::getInstance());
264     extensions.initWithGLStrings(
265             glGetString(GL_VENDOR),
266             glGetString(GL_RENDERER),
267             glGetString(GL_VERSION),
268             glGetString(GL_EXTENSIONS),
269             eglQueryString(display, EGL_VENDOR),
270             eglQueryString(display, EGL_VERSION),
271             eglQueryString(display, EGL_EXTENSIONS));
272 
273     glGetIntegerv(GL_MAX_TEXTURE_SIZE, &mMaxTextureSize);
274     glGetIntegerv(GL_MAX_VIEWPORT_DIMS, mMaxViewportDims);
275 
276 
277 #ifdef EGL_ANDROID_swap_rectangle
278     if (extensions.hasExtension("EGL_ANDROID_swap_rectangle")) {
279         if (eglSetSwapRectangleANDROID(display, surface,
280                 0, 0, mWidth, mHeight) == EGL_TRUE) {
281             // This could fail if this extension is not supported by this
282             // specific surface (of config)
283             mFlags |= SWAP_RECTANGLE;
284         }
285     }
286     // when we have the choice between PARTIAL_UPDATES and SWAP_RECTANGLE
287     // choose PARTIAL_UPDATES, which should be more efficient
288     if (mFlags & PARTIAL_UPDATES)
289         mFlags &= ~SWAP_RECTANGLE;
290 #endif
291 
292     LOGI("EGL informations:");
293     LOGI("# of configs : %d", numConfigs);
294     LOGI("vendor    : %s", extensions.getEglVendor());
295     LOGI("version   : %s", extensions.getEglVersion());
296     LOGI("extensions: %s", extensions.getEglExtension());
297     LOGI("Client API: %s", eglQueryString(display, EGL_CLIENT_APIS)?:"Not Supported");
298     LOGI("EGLSurface: %d-%d-%d-%d, config=%p", r, g, b, a, config);
299 
300     LOGI("OpenGL informations:");
301     LOGI("vendor    : %s", extensions.getVendor());
302     LOGI("renderer  : %s", extensions.getRenderer());
303     LOGI("version   : %s", extensions.getVersion());
304     LOGI("extensions: %s", extensions.getExtension());
305     LOGI("GL_MAX_TEXTURE_SIZE = %d", mMaxTextureSize);
306     LOGI("GL_MAX_VIEWPORT_DIMS = %d x %d", mMaxViewportDims[0], mMaxViewportDims[1]);
307     LOGI("flags = %08x", mFlags);
308 
309     // Unbind the context from this thread
310     eglMakeCurrent(display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
311 
312 
313     // initialize the H/W composer
314     mHwc = new HWComposer(mFlinger);
315     if (mHwc->initCheck() == NO_ERROR) {
316         mHwc->setFrameBuffer(mDisplay, mSurface);
317     }
318 }
319 
getHwComposer() const320 HWComposer& DisplayHardware::getHwComposer() const {
321     return *mHwc;
322 }
323 
324 /*
325  * Clean up.  Throw out our local state.
326  *
327  * (It's entirely possible we'll never get here, since this is meant
328  * for real hardware, which doesn't restart.)
329  */
330 
fini()331 void DisplayHardware::fini()
332 {
333     eglMakeCurrent(mDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
334     eglTerminate(mDisplay);
335 }
336 
releaseScreen() const337 void DisplayHardware::releaseScreen() const
338 {
339     DisplayHardwareBase::releaseScreen();
340     if (mHwc->initCheck() == NO_ERROR) {
341         mHwc->release();
342     }
343 }
344 
acquireScreen() const345 void DisplayHardware::acquireScreen() const
346 {
347     DisplayHardwareBase::acquireScreen();
348 }
349 
getPageFlipCount() const350 uint32_t DisplayHardware::getPageFlipCount() const {
351     return mPageFlipCount;
352 }
353 
compositionComplete() const354 status_t DisplayHardware::compositionComplete() const {
355     return mNativeWindow->compositionComplete();
356 }
357 
getCurrentBufferIndex() const358 int DisplayHardware::getCurrentBufferIndex() const {
359     return mNativeWindow->getCurrentBufferIndex();
360 }
361 
flip(const Region & dirty) const362 void DisplayHardware::flip(const Region& dirty) const
363 {
364     checkGLErrors();
365 
366     EGLDisplay dpy = mDisplay;
367     EGLSurface surface = mSurface;
368 
369 #ifdef EGL_ANDROID_swap_rectangle
370     if (mFlags & SWAP_RECTANGLE) {
371         const Region newDirty(dirty.intersect(bounds()));
372         const Rect b(newDirty.getBounds());
373         eglSetSwapRectangleANDROID(dpy, surface,
374                 b.left, b.top, b.width(), b.height());
375     }
376 #endif
377 
378     if (mFlags & PARTIAL_UPDATES) {
379         mNativeWindow->setUpdateRectangle(dirty.getBounds());
380     }
381 
382     mPageFlipCount++;
383 
384     if (mHwc->initCheck() == NO_ERROR) {
385         mHwc->commit();
386     } else {
387         eglSwapBuffers(dpy, surface);
388     }
389     checkEGLErrors("eglSwapBuffers");
390 
391     // for debugging
392     //glClearColor(1,0,0,0);
393     //glClear(GL_COLOR_BUFFER_BIT);
394 }
395 
getFlags() const396 uint32_t DisplayHardware::getFlags() const
397 {
398     return mFlags;
399 }
400 
makeCurrent() const401 void DisplayHardware::makeCurrent() const
402 {
403     eglMakeCurrent(mDisplay, mSurface, mSurface, mContext);
404 }
405 
dump(String8 & res) const406 void DisplayHardware::dump(String8& res) const
407 {
408     mNativeWindow->dump(res);
409 }
410