DisplayDevice.cpp revision 8b736f138cfd9b239a2c7073347a13c489534ae1
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      mDisplayWidth(), mDisplayHeight(), mFormat(),
83      mFlags(),
84      mPageFlipCount(),
85      mSecureLayerVisible(false),
86      mScreenAcquired(false),
87      mOrientation(),
88      mLayerStack(0)
89{
90    init(config);
91}
92
93DisplayDevice::~DisplayDevice() {
94    if (mSurface != EGL_NO_SURFACE) {
95        eglDestroySurface(mDisplay, mSurface);
96        mSurface = EGL_NO_SURFACE;
97    }
98}
99
100bool DisplayDevice::isValid() const {
101    return mFlinger != NULL;
102}
103
104int DisplayDevice::getWidth() const {
105    return mDisplayWidth;
106}
107
108int DisplayDevice::getHeight() const {
109    return mDisplayHeight;
110}
111
112PixelFormat DisplayDevice::getFormat() const {
113    return mFormat;
114}
115
116EGLSurface DisplayDevice::getEGLSurface() const {
117    return mSurface;
118}
119
120void DisplayDevice::init(EGLConfig config)
121{
122    ANativeWindow* const window = mNativeWindow.get();
123
124    int format;
125    window->query(window, NATIVE_WINDOW_FORMAT, &format);
126
127    /*
128     * Create our display's surface
129     */
130
131    EGLSurface surface;
132    EGLint w, h;
133    EGLDisplay display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
134    surface = eglCreateWindowSurface(display, config, window, NULL);
135    eglQuerySurface(display, surface, EGL_WIDTH,  &mDisplayWidth);
136    eglQuerySurface(display, surface, EGL_HEIGHT, &mDisplayHeight);
137
138    mDisplay = display;
139    mSurface = surface;
140    mFormat  = format;
141    mPageFlipCount = 0;
142
143    // initialize the display orientation transform.
144    DisplayDevice::setOrientation(DisplayState::eOrientationDefault);
145}
146
147uint32_t DisplayDevice::getPageFlipCount() const {
148    return mPageFlipCount;
149}
150
151status_t DisplayDevice::compositionComplete() const {
152    if (mFramebufferSurface == NULL) {
153        return NO_ERROR;
154    }
155    return mFramebufferSurface->compositionComplete();
156}
157
158void DisplayDevice::flip(const Region& dirty) const
159{
160    checkGLErrors();
161
162    EGLDisplay dpy = mDisplay;
163    EGLSurface surface = mSurface;
164
165#ifdef EGL_ANDROID_swap_rectangle
166    if (mFlags & SWAP_RECTANGLE) {
167        const Region newDirty(dirty.intersect(bounds()));
168        const Rect b(newDirty.getBounds());
169        eglSetSwapRectangleANDROID(dpy, surface,
170                b.left, b.top, b.width(), b.height());
171    }
172#endif
173
174    mPageFlipCount++;
175}
176
177uint32_t DisplayDevice::getFlags() const
178{
179    return mFlags;
180}
181
182void DisplayDevice::dump(String8& res) const
183{
184    if (mFramebufferSurface != NULL) {
185        mFramebufferSurface->dump(res);
186    }
187}
188
189void DisplayDevice::makeCurrent(const sp<const DisplayDevice>& hw, EGLContext ctx) {
190    EGLSurface sur = eglGetCurrentSurface(EGL_DRAW);
191    if (sur != hw->mSurface) {
192        EGLDisplay dpy = eglGetCurrentDisplay();
193        eglMakeCurrent(dpy, hw->mSurface, hw->mSurface, ctx);
194    }
195}
196
197// ----------------------------------------------------------------------------
198
199void DisplayDevice::setVisibleLayersSortedByZ(const Vector< sp<LayerBase> >& layers) {
200    mVisibleLayersSortedByZ = layers;
201    mSecureLayerVisible = false;
202    size_t count = layers.size();
203    for (size_t i=0 ; i<count ; i++) {
204        if (layers[i]->isSecure()) {
205            mSecureLayerVisible = true;
206        }
207    }
208}
209
210Vector< sp<LayerBase> > DisplayDevice::getVisibleLayersSortedByZ() const {
211    return mVisibleLayersSortedByZ;
212}
213
214bool DisplayDevice::getSecureLayerVisible() const {
215    return mSecureLayerVisible;
216}
217
218// ----------------------------------------------------------------------------
219
220bool DisplayDevice::canDraw() const {
221    return mScreenAcquired;
222}
223
224void DisplayDevice::releaseScreen() const {
225    mScreenAcquired = false;
226}
227
228void DisplayDevice::acquireScreen() const {
229    mScreenAcquired = true;
230}
231
232bool DisplayDevice::isScreenAcquired() const {
233    return mScreenAcquired;
234}
235
236// ----------------------------------------------------------------------------
237
238void DisplayDevice::setLayerStack(uint32_t stack) {
239    mLayerStack = stack;
240    dirtyRegion.set(bounds());
241}
242
243// ----------------------------------------------------------------------------
244
245status_t DisplayDevice::orientationToTransfrom(
246        int orientation, int w, int h, Transform* tr)
247{
248    uint32_t flags = 0;
249    switch (orientation) {
250    case DisplayState::eOrientationDefault:
251        flags = Transform::ROT_0;
252        break;
253    case DisplayState::eOrientation90:
254        flags = Transform::ROT_90;
255        break;
256    case DisplayState::eOrientation180:
257        flags = Transform::ROT_180;
258        break;
259    case DisplayState::eOrientation270:
260        flags = Transform::ROT_270;
261        break;
262    default:
263        return BAD_VALUE;
264    }
265    tr->set(flags, w, h);
266    return NO_ERROR;
267}
268
269status_t DisplayDevice::setOrientation(int orientation) {
270    int w = mDisplayWidth;
271    int h = mDisplayHeight;
272
273    DisplayDevice::orientationToTransfrom(
274            orientation, w, h, &mGlobalTransform);
275    if (orientation & DisplayState::eOrientationSwapMask) {
276        int tmp = w;
277        w = h;
278        h = tmp;
279    }
280    mOrientation = orientation;
281    dirtyRegion.set(bounds());
282    return NO_ERROR;
283}
284