2 * Copyright (C) 2007 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
22 #include <cutils/properties.h>
24 #include <utils/RefBase.h>
25 #include <utils/Log.h>
27 #include <ui/DisplayInfo.h>
28 #include <ui/PixelFormat.h>
30 #include <gui/SurfaceTextureClient.h>
34 #include <EGL/eglext.h>
36 #include <hardware/gralloc.h>
38 #include "DisplayHardware/FramebufferSurface.h"
39 #include "DisplayHardware/HWComposer.h"
41 #include "DisplayDevice.h"
42 #include "GLExtensions.h"
43 #include "SurfaceFlinger.h"
44 #include "LayerBase.h"
46 // ----------------------------------------------------------------------------
47 using namespace android;
48 // ----------------------------------------------------------------------------
50 static __attribute__((noinline))
54 // there could be more than one error flag
55 GLenum error = glGetError();
56 if (error == GL_NO_ERROR)
58 ALOGE("GL error 0x%04x", int(error));
62 static __attribute__((noinline))
63 void checkEGLErrors(const char* token)
66 static const char *strerror(EGLint err) {
68 case EGL_SUCCESS: return "EGL_SUCCESS";
69 case EGL_NOT_INITIALIZED: return "EGL_NOT_INITIALIZED";
70 case EGL_BAD_ACCESS: return "EGL_BAD_ACCESS";
71 case EGL_BAD_ALLOC: return "EGL_BAD_ALLOC";
72 case EGL_BAD_ATTRIBUTE: return "EGL_BAD_ATTRIBUTE";
73 case EGL_BAD_CONFIG: return "EGL_BAD_CONFIG";
74 case EGL_BAD_CONTEXT: return "EGL_BAD_CONTEXT";
75 case EGL_BAD_CURRENT_SURFACE: return "EGL_BAD_CURRENT_SURFACE";
76 case EGL_BAD_DISPLAY: return "EGL_BAD_DISPLAY";
77 case EGL_BAD_MATCH: return "EGL_BAD_MATCH";
78 case EGL_BAD_NATIVE_PIXMAP: return "EGL_BAD_NATIVE_PIXMAP";
79 case EGL_BAD_NATIVE_WINDOW: return "EGL_BAD_NATIVE_WINDOW";
80 case EGL_BAD_PARAMETER: return "EGL_BAD_PARAMETER";
81 case EGL_BAD_SURFACE: return "EGL_BAD_SURFACE";
82 case EGL_CONTEXT_LOST: return "EGL_CONTEXT_LOST";
83 default: return "UNKNOWN";
88 EGLint error = eglGetError();
89 if (error && error != EGL_SUCCESS) {
90 ALOGE("%s: EGL error 0x%04x (%s)",
91 token, int(error), EGLUtils::strerror(error));
95 // ----------------------------------------------------------------------------
98 * Initialize the display to the specified values.
102 DisplayDevice::DisplayDevice(
103 const sp<SurfaceFlinger>& flinger,
105 const sp<ANativeWindow>& nativeWindow,
106 const sp<FramebufferSurface>& framebufferSurface,
110 mNativeWindow(nativeWindow),
111 mFramebufferSurface(framebufferSurface),
112 mDisplay(EGL_NO_DISPLAY),
113 mSurface(EGL_NO_SURFACE),
114 mContext(EGL_NO_CONTEXT),
117 mDisplayWidth(), mDisplayHeight(), mFormat(),
120 mSecureLayerVisible(false),
121 mScreenAcquired(false),
128 DisplayDevice::~DisplayDevice() {
129 if (mSurface != EGL_NO_SURFACE) {
130 eglDestroySurface(mDisplay, mSurface);
131 mSurface = EGL_NO_SURFACE;
135 bool DisplayDevice::isValid() const {
136 return mFlinger != NULL;
139 float DisplayDevice::getDpiX() const {
143 float DisplayDevice::getDpiY() const {
147 float DisplayDevice::getDensity() const {
151 int DisplayDevice::getWidth() const {
152 return mDisplayWidth;
155 int DisplayDevice::getHeight() const {
156 return mDisplayHeight;
159 PixelFormat DisplayDevice::getFormat() const {
163 EGLSurface DisplayDevice::getEGLSurface() const {
167 void DisplayDevice::init(EGLConfig config)
169 ANativeWindow* const window = mNativeWindow.get();
172 window->query(window, NATIVE_WINDOW_FORMAT, &format);
173 mDpiX = window->xdpi;
174 mDpiY = window->ydpi;
176 // TODO: Not sure if display density should handled by SF any longer
178 static int getDensityFromProperty(char const* propName) {
179 char property[PROPERTY_VALUE_MAX];
181 if (property_get(propName, property, NULL) > 0) {
182 density = atoi(property);
187 static int getEmuDensity() {
188 return getDensityFromProperty("qemu.sf.lcd_density"); }
189 static int getBuildDensity() {
190 return getDensityFromProperty("ro.sf.lcd_density"); }
192 // The density of the device is provided by a build property
193 mDensity = Density::getBuildDensity() / 160.0f;
195 // the build doesn't provide a density -- this is wrong!
197 ALOGE("ro.sf.lcd_density must be defined as a build property");
198 mDensity = mDpiX / 160.0f;
200 if (Density::getEmuDensity()) {
201 // if "qemu.sf.lcd_density" is specified, it overrides everything
202 mDpiX = mDpiY = mDensity = Density::getEmuDensity();
207 * Create our display's surface
212 EGLDisplay display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
213 surface = eglCreateWindowSurface(display, config, window, NULL);
214 eglQuerySurface(display, surface, EGL_WIDTH, &mDisplayWidth);
215 eglQuerySurface(display, surface, EGL_HEIGHT, &mDisplayHeight);
222 // initialize the display orientation transform.
223 DisplayDevice::setOrientation(ISurfaceComposer::eOrientationDefault);
226 uint32_t DisplayDevice::getPageFlipCount() const {
227 return mPageFlipCount;
230 status_t DisplayDevice::compositionComplete() const {
231 if (mFramebufferSurface == NULL) {
234 return mFramebufferSurface->compositionComplete();
237 void DisplayDevice::flip(const Region& dirty) const
241 EGLDisplay dpy = mDisplay;
242 EGLSurface surface = mSurface;
244 #ifdef EGL_ANDROID_swap_rectangle
245 if (mFlags & SWAP_RECTANGLE) {
246 const Region newDirty(dirty.intersect(bounds()));
247 const Rect b(newDirty.getBounds());
248 eglSetSwapRectangleANDROID(dpy, surface,
249 b.left, b.top, b.width(), b.height());
256 uint32_t DisplayDevice::getFlags() const
261 void DisplayDevice::dump(String8& res) const
263 if (mFramebufferSurface != NULL) {
264 mFramebufferSurface->dump(res);
268 void DisplayDevice::makeCurrent(const sp<const DisplayDevice>& hw, EGLContext ctx) {
269 EGLSurface sur = eglGetCurrentSurface(EGL_DRAW);
270 if (sur != hw->mSurface) {
271 EGLDisplay dpy = eglGetCurrentDisplay();
272 eglMakeCurrent(dpy, hw->mSurface, hw->mSurface, ctx);
276 // ----------------------------------------------------------------------------
278 void DisplayDevice::setVisibleLayersSortedByZ(const Vector< sp<LayerBase> >& layers) {
279 mVisibleLayersSortedByZ = layers;
280 size_t count = layers.size();
281 for (size_t i=0 ; i<count ; i++) {
282 if (layers[i]->isSecure()) {
283 mSecureLayerVisible = true;
288 Vector< sp<LayerBase> > DisplayDevice::getVisibleLayersSortedByZ() const {
289 return mVisibleLayersSortedByZ;
292 bool DisplayDevice::getSecureLayerVisible() const {
293 return mSecureLayerVisible;
296 // ----------------------------------------------------------------------------
298 bool DisplayDevice::canDraw() const {
299 return mScreenAcquired;
302 void DisplayDevice::releaseScreen() const {
303 mScreenAcquired = false;
306 void DisplayDevice::acquireScreen() const {
307 mScreenAcquired = true;
310 bool DisplayDevice::isScreenAcquired() const {
311 return mScreenAcquired;
314 // ----------------------------------------------------------------------------
316 void DisplayDevice::setLayerStack(uint32_t stack) {
318 dirtyRegion.set(bounds());
321 // ----------------------------------------------------------------------------
323 status_t DisplayDevice::orientationToTransfrom(
324 int orientation, int w, int h, Transform* tr)
327 switch (orientation) {
328 case ISurfaceComposer::eOrientationDefault:
329 flags = Transform::ROT_0;
331 case ISurfaceComposer::eOrientation90:
332 flags = Transform::ROT_90;
334 case ISurfaceComposer::eOrientation180:
335 flags = Transform::ROT_180;
337 case ISurfaceComposer::eOrientation270:
338 flags = Transform::ROT_270;
343 tr->set(flags, w, h);
347 status_t DisplayDevice::setOrientation(int orientation) {
348 int w = mDisplayWidth;
349 int h = mDisplayHeight;
351 DisplayDevice::orientationToTransfrom(
352 orientation, w, h, &mGlobalTransform);
353 if (orientation & ISurfaceComposer::eOrientationSwapMask) {
358 mOrientation = orientation;
359 dirtyRegion.set(bounds());