DisplayDevice.cpp revision ef7b9c7eac036cc1230c64821039d18f8cbd2c1c
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/DisplayInfo.h>
28#include <ui/PixelFormat.h>
29
30#include <gui/SurfaceTextureClient.h>
31
32#include <GLES/gl.h>
33#include <EGL/egl.h>
34#include <EGL/eglext.h>
35
36#include <hardware/gralloc.h>
37
38#include "DisplayHardware/FramebufferSurface.h"
39#include "DisplayHardware/HWComposer.h"
40
41#include "DisplayDevice.h"
42#include "GLExtensions.h"
43#include "SurfaceFlinger.h"
44#include "LayerBase.h"
45
46// ----------------------------------------------------------------------------
47using namespace android;
48// ----------------------------------------------------------------------------
49
50static __attribute__((noinline))
51void checkGLErrors()
52{
53    do {
54        // there could be more than one error flag
55        GLenum error = glGetError();
56        if (error == GL_NO_ERROR)
57            break;
58        ALOGE("GL error 0x%04x", int(error));
59    } while(true);
60}
61
62// ----------------------------------------------------------------------------
63
64/*
65 * Initialize the display to the specified values.
66 *
67 */
68
69DisplayDevice::DisplayDevice(
70        const sp<SurfaceFlinger>& flinger,
71        int display,
72        const sp<ANativeWindow>& nativeWindow,
73        const sp<FramebufferSurface>& framebufferSurface,
74        EGLConfig config)
75    : mFlinger(flinger),
76      mId(display),
77      mNativeWindow(nativeWindow),
78      mFramebufferSurface(framebufferSurface),
79      mDisplay(EGL_NO_DISPLAY),
80      mSurface(EGL_NO_SURFACE),
81      mContext(EGL_NO_CONTEXT),
82      mDpiX(), mDpiY(),
83      mDensity(),
84      mDisplayWidth(), mDisplayHeight(), mFormat(),
85      mFlags(),
86      mPageFlipCount(),
87      mSecureLayerVisible(false),
88      mScreenAcquired(false),
89      mOrientation(),
90      mLayerStack(0)
91{
92    init(config);
93}
94
95DisplayDevice::~DisplayDevice() {
96    if (mSurface != EGL_NO_SURFACE) {
97        eglDestroySurface(mDisplay, mSurface);
98        mSurface = EGL_NO_SURFACE;
99    }
100}
101
102bool DisplayDevice::isValid() const {
103    return mFlinger != NULL;
104}
105
106float DisplayDevice::getDpiX() const {
107    return mDpiX;
108}
109
110float DisplayDevice::getDpiY() const {
111    return mDpiY;
112}
113
114float DisplayDevice::getDensity() const {
115    return mDensity;
116}
117
118int DisplayDevice::getWidth() const {
119    return mDisplayWidth;
120}
121
122int DisplayDevice::getHeight() const {
123    return mDisplayHeight;
124}
125
126PixelFormat DisplayDevice::getFormat() const {
127    return mFormat;
128}
129
130EGLSurface DisplayDevice::getEGLSurface() const {
131    return mSurface;
132}
133
134void DisplayDevice::init(EGLConfig config)
135{
136    ANativeWindow* const window = mNativeWindow.get();
137
138    int format;
139    window->query(window, NATIVE_WINDOW_FORMAT, &format);
140    mDpiX = window->xdpi;
141    mDpiY = window->ydpi;
142
143    // TODO: Not sure if display density should handled by SF any longer
144    class Density {
145        static int getDensityFromProperty(char const* propName) {
146            char property[PROPERTY_VALUE_MAX];
147            int density = 0;
148            if (property_get(propName, property, NULL) > 0) {
149                density = atoi(property);
150            }
151            return density;
152        }
153    public:
154        static int getEmuDensity() {
155            return getDensityFromProperty("qemu.sf.lcd_density"); }
156        static int getBuildDensity()  {
157            return getDensityFromProperty("ro.sf.lcd_density"); }
158    };
159    // The density of the device is provided by a build property
160    mDensity = Density::getBuildDensity() / 160.0f;
161    if (mDensity == 0) {
162        // the build doesn't provide a density -- this is wrong!
163        // use xdpi instead
164        ALOGE("ro.sf.lcd_density must be defined as a build property");
165        mDensity = mDpiX / 160.0f;
166    }
167    if (Density::getEmuDensity()) {
168        // if "qemu.sf.lcd_density" is specified, it overrides everything
169        mDpiX = mDpiY = mDensity = Density::getEmuDensity();
170        mDensity /= 160.0f;
171    }
172
173    /*
174     * Create our display's surface
175     */
176
177    EGLSurface surface;
178    EGLint w, h;
179    EGLDisplay display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
180    surface = eglCreateWindowSurface(display, config, window, NULL);
181    eglQuerySurface(display, surface, EGL_WIDTH,  &mDisplayWidth);
182    eglQuerySurface(display, surface, EGL_HEIGHT, &mDisplayHeight);
183
184    mDisplay = display;
185    mSurface = surface;
186    mFormat  = format;
187    mPageFlipCount = 0;
188
189    // initialize the display orientation transform.
190    DisplayDevice::setOrientation(DisplayState::eOrientationDefault);
191}
192
193uint32_t DisplayDevice::getPageFlipCount() const {
194    return mPageFlipCount;
195}
196
197status_t DisplayDevice::compositionComplete() const {
198    if (mFramebufferSurface == NULL) {
199        return NO_ERROR;
200    }
201    return mFramebufferSurface->compositionComplete();
202}
203
204void DisplayDevice::flip(const Region& dirty) const
205{
206    checkGLErrors();
207
208    EGLDisplay dpy = mDisplay;
209    EGLSurface surface = mSurface;
210
211#ifdef EGL_ANDROID_swap_rectangle
212    if (mFlags & SWAP_RECTANGLE) {
213        const Region newDirty(dirty.intersect(bounds()));
214        const Rect b(newDirty.getBounds());
215        eglSetSwapRectangleANDROID(dpy, surface,
216                b.left, b.top, b.width(), b.height());
217    }
218#endif
219
220    mPageFlipCount++;
221}
222
223uint32_t DisplayDevice::getFlags() const
224{
225    return mFlags;
226}
227
228void DisplayDevice::dump(String8& res) const
229{
230    if (mFramebufferSurface != NULL) {
231        mFramebufferSurface->dump(res);
232    }
233}
234
235void DisplayDevice::makeCurrent(const sp<const DisplayDevice>& hw, EGLContext ctx) {
236    EGLSurface sur = eglGetCurrentSurface(EGL_DRAW);
237    if (sur != hw->mSurface) {
238        EGLDisplay dpy = eglGetCurrentDisplay();
239        eglMakeCurrent(dpy, hw->mSurface, hw->mSurface, ctx);
240    }
241}
242
243// ----------------------------------------------------------------------------
244
245void DisplayDevice::setVisibleLayersSortedByZ(const Vector< sp<LayerBase> >& layers) {
246    mVisibleLayersSortedByZ = layers;
247    mSecureLayerVisible = false;
248    size_t count = layers.size();
249    for (size_t i=0 ; i<count ; i++) {
250        if (layers[i]->isSecure()) {
251            mSecureLayerVisible = true;
252        }
253    }
254}
255
256Vector< sp<LayerBase> > DisplayDevice::getVisibleLayersSortedByZ() const {
257    return mVisibleLayersSortedByZ;
258}
259
260bool DisplayDevice::getSecureLayerVisible() const {
261    return mSecureLayerVisible;
262}
263
264// ----------------------------------------------------------------------------
265
266bool DisplayDevice::canDraw() const {
267    return mScreenAcquired;
268}
269
270void DisplayDevice::releaseScreen() const {
271    mScreenAcquired = false;
272}
273
274void DisplayDevice::acquireScreen() const {
275    mScreenAcquired = true;
276}
277
278bool DisplayDevice::isScreenAcquired() const {
279    return mScreenAcquired;
280}
281
282// ----------------------------------------------------------------------------
283
284void DisplayDevice::setLayerStack(uint32_t stack) {
285    mLayerStack = stack;
286    dirtyRegion.set(bounds());
287}
288
289// ----------------------------------------------------------------------------
290
291status_t DisplayDevice::orientationToTransfrom(
292        int orientation, int w, int h, Transform* tr)
293{
294    uint32_t flags = 0;
295    switch (orientation) {
296    case DisplayState::eOrientationDefault:
297        flags = Transform::ROT_0;
298        break;
299    case DisplayState::eOrientation90:
300        flags = Transform::ROT_90;
301        break;
302    case DisplayState::eOrientation180:
303        flags = Transform::ROT_180;
304        break;
305    case DisplayState::eOrientation270:
306        flags = Transform::ROT_270;
307        break;
308    default:
309        return BAD_VALUE;
310    }
311    tr->set(flags, w, h);
312    return NO_ERROR;
313}
314
315status_t DisplayDevice::setOrientation(int orientation) {
316    int w = mDisplayWidth;
317    int h = mDisplayHeight;
318
319    DisplayDevice::orientationToTransfrom(
320            orientation, w, h, &mGlobalTransform);
321    if (orientation & DisplayState::eOrientationSwapMask) {
322        int tmp = w;
323        w = h;
324        h = tmp;
325    }
326    mOrientation = orientation;
327    dirtyRegion.set(bounds());
328    return NO_ERROR;
329}
330