DisplayDevice.cpp revision d870703d5566490cfdfb389d9336b2b8d3c6cc7a
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 "clz.h"
42#include "DisplayDevice.h"
43#include "GLExtensions.h"
44#include "SurfaceFlinger.h"
45#include "LayerBase.h"
46
47// ----------------------------------------------------------------------------
48using namespace android;
49// ----------------------------------------------------------------------------
50
51static __attribute__((noinline))
52void checkGLErrors()
53{
54    do {
55        // there could be more than one error flag
56        GLenum error = glGetError();
57        if (error == GL_NO_ERROR)
58            break;
59        ALOGE("GL error 0x%04x", int(error));
60    } while(true);
61}
62
63// ----------------------------------------------------------------------------
64
65/*
66 * Initialize the display to the specified values.
67 *
68 */
69
70DisplayDevice::DisplayDevice(
71        const sp<SurfaceFlinger>& flinger,
72        DisplayType type, const wp<IBinder>& displayToken,
73        const sp<ANativeWindow>& nativeWindow,
74        const sp<FramebufferSurface>& framebufferSurface,
75        EGLConfig config)
76    : mFlinger(flinger),
77      mType(type), mHwcDisplayId(-1),
78      mNativeWindow(nativeWindow),
79      mFramebufferSurface(framebufferSurface),
80      mDisplay(EGL_NO_DISPLAY),
81      mSurface(EGL_NO_SURFACE),
82      mContext(EGL_NO_CONTEXT),
83      mDisplayWidth(), mDisplayHeight(), mFormat(),
84      mFlags(),
85      mPageFlipCount(),
86      mSecureLayerVisible(false),
87      mScreenAcquired(false),
88      mLayerStack(0),
89      mOrientation()
90{
91    init(config);
92}
93
94DisplayDevice::~DisplayDevice() {
95    if (mSurface != EGL_NO_SURFACE) {
96        eglDestroySurface(mDisplay, mSurface);
97        mSurface = EGL_NO_SURFACE;
98    }
99}
100
101bool DisplayDevice::isValid() const {
102    return mFlinger != NULL;
103}
104
105int DisplayDevice::getWidth() const {
106    return mDisplayWidth;
107}
108
109int DisplayDevice::getHeight() const {
110    return mDisplayHeight;
111}
112
113PixelFormat DisplayDevice::getFormat() const {
114    return mFormat;
115}
116
117EGLSurface DisplayDevice::getEGLSurface() const {
118    return mSurface;
119}
120
121void DisplayDevice::init(EGLConfig config)
122{
123    ANativeWindow* const window = mNativeWindow.get();
124
125    int format;
126    window->query(window, NATIVE_WINDOW_FORMAT, &format);
127
128    /*
129     * Create our display's surface
130     */
131
132    EGLSurface surface;
133    EGLint w, h;
134    EGLDisplay display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
135    surface = eglCreateWindowSurface(display, config, window, NULL);
136    eglQuerySurface(display, surface, EGL_WIDTH,  &mDisplayWidth);
137    eglQuerySurface(display, surface, EGL_HEIGHT, &mDisplayHeight);
138
139    mDisplay = display;
140    mSurface = surface;
141    mFormat  = format;
142    mPageFlipCount = 0;
143    mViewport.makeInvalid();
144    mFrame.makeInvalid();
145
146    // external displays are always considered enabled
147    mScreenAcquired = (mType >= DisplayDevice::NUM_DISPLAY_TYPES);
148
149    // get an h/w composer ID
150    mHwcDisplayId = mFlinger->allocateHwcDisplayId(mType);
151
152    // initialize the display orientation transform.
153    setProjection(DisplayState::eOrientationDefault, mViewport, mFrame);
154}
155
156uint32_t DisplayDevice::getPageFlipCount() const {
157    return mPageFlipCount;
158}
159
160status_t DisplayDevice::compositionComplete() const {
161    if (mFramebufferSurface == NULL) {
162        return NO_ERROR;
163    }
164    return mFramebufferSurface->compositionComplete();
165}
166
167void DisplayDevice::flip(const Region& dirty) const
168{
169    checkGLErrors();
170
171    EGLDisplay dpy = mDisplay;
172    EGLSurface surface = mSurface;
173
174#ifdef EGL_ANDROID_swap_rectangle
175    if (mFlags & SWAP_RECTANGLE) {
176        const Region newDirty(dirty.intersect(bounds()));
177        const Rect b(newDirty.getBounds());
178        eglSetSwapRectangleANDROID(dpy, surface,
179                b.left, b.top, b.width(), b.height());
180    }
181#endif
182
183    mPageFlipCount++;
184}
185
186void DisplayDevice::swapBuffers(HWComposer& hwc) const {
187    if (hwc.initCheck() != NO_ERROR) {
188        // no HWC, we call eglSwapBuffers()
189        eglSwapBuffers(mDisplay, mSurface);
190    } else {
191        // We have a valid HWC, but not all displays can use it, in particular
192        // the virtual displays are on their own.
193        // TODO: HWC 1.2 will allow virtual displays
194        if (mType >= DisplayDevice::DISPLAY_VIRTUAL) {
195            // always call eglSwapBuffers() for virtual displays
196            eglSwapBuffers(mDisplay, mSurface);
197        } else if (hwc.supportsFramebufferTarget()) {
198            // as of hwc 1.1 we always call eglSwapBuffers if we have some
199            // GLES layers
200            if (hwc.hasGlesComposition(mType)) {
201                eglSwapBuffers(mDisplay, mSurface);
202            }
203        } else {
204            // HWC doesn't have the framebuffer target, we don't call
205            // eglSwapBuffers(), since this is handled by HWComposer::commit().
206        }
207    }
208}
209
210void DisplayDevice::onSwapBuffersCompleted(HWComposer& hwc) const {
211    if (hwc.initCheck() == NO_ERROR) {
212        if (hwc.supportsFramebufferTarget()) {
213            int fd = hwc.getAndResetReleaseFenceFd(mType);
214            mFramebufferSurface->setReleaseFenceFd(fd);
215        }
216    }
217}
218
219uint32_t DisplayDevice::getFlags() const
220{
221    return mFlags;
222}
223
224void DisplayDevice::dump(String8& res) const
225{
226    if (mFramebufferSurface != NULL) {
227        mFramebufferSurface->dump(res);
228    }
229}
230
231EGLBoolean DisplayDevice::makeCurrent(EGLDisplay dpy,
232        const sp<const DisplayDevice>& hw, EGLContext ctx) {
233    EGLBoolean result = EGL_TRUE;
234    EGLSurface sur = eglGetCurrentSurface(EGL_DRAW);
235    if (sur != hw->mSurface) {
236        result = eglMakeCurrent(dpy, hw->mSurface, hw->mSurface, ctx);
237        if (result == EGL_TRUE) {
238            GLsizei w = hw->mDisplayWidth;
239            GLsizei h = hw->mDisplayHeight;
240            glViewport(0, 0, w, h);
241            glMatrixMode(GL_PROJECTION);
242            glLoadIdentity();
243            // put the origin in the left-bottom corner
244            glOrthof(0, w, 0, h, 0, 1); // l=0, r=w ; b=0, t=h
245        }
246    }
247    return result;
248}
249
250// ----------------------------------------------------------------------------
251
252void DisplayDevice::setVisibleLayersSortedByZ(const Vector< sp<LayerBase> >& layers) {
253    mVisibleLayersSortedByZ = layers;
254    mSecureLayerVisible = false;
255    size_t count = layers.size();
256    for (size_t i=0 ; i<count ; i++) {
257        if (layers[i]->isSecure()) {
258            mSecureLayerVisible = true;
259        }
260    }
261}
262
263const Vector< sp<LayerBase> >& DisplayDevice::getVisibleLayersSortedByZ() const {
264    return mVisibleLayersSortedByZ;
265}
266
267bool DisplayDevice::getSecureLayerVisible() const {
268    return mSecureLayerVisible;
269}
270
271Region DisplayDevice::getDirtyRegion(bool repaintEverything) const {
272    Region dirty;
273    if (repaintEverything) {
274        dirty.set(getBounds());
275    } else {
276        const Transform& planeTransform(mGlobalTransform);
277        dirty = planeTransform.transform(this->dirtyRegion);
278        dirty.andSelf(getBounds());
279    }
280    return dirty;
281}
282
283// ----------------------------------------------------------------------------
284
285bool DisplayDevice::canDraw() const {
286    return mScreenAcquired;
287}
288
289void DisplayDevice::releaseScreen() const {
290    mScreenAcquired = false;
291}
292
293void DisplayDevice::acquireScreen() const {
294    mScreenAcquired = true;
295}
296
297bool DisplayDevice::isScreenAcquired() const {
298    return mScreenAcquired;
299}
300
301// ----------------------------------------------------------------------------
302
303void DisplayDevice::setLayerStack(uint32_t stack) {
304    mLayerStack = stack;
305    dirtyRegion.set(bounds());
306}
307
308// ----------------------------------------------------------------------------
309
310status_t DisplayDevice::orientationToTransfrom(
311        int orientation, int w, int h, Transform* tr)
312{
313    uint32_t flags = 0;
314    switch (orientation) {
315    case DisplayState::eOrientationDefault:
316        flags = Transform::ROT_0;
317        break;
318    case DisplayState::eOrientation90:
319        flags = Transform::ROT_90;
320        break;
321    case DisplayState::eOrientation180:
322        flags = Transform::ROT_180;
323        break;
324    case DisplayState::eOrientation270:
325        flags = Transform::ROT_270;
326        break;
327    default:
328        return BAD_VALUE;
329    }
330    tr->set(flags, w, h);
331    return NO_ERROR;
332}
333
334void DisplayDevice::setProjection(int orientation,
335        const Rect& viewport, const Rect& frame) {
336    mOrientation = orientation;
337    mViewport = viewport;
338    mFrame = frame;
339    updateGeometryTransform();
340}
341
342void DisplayDevice::updateGeometryTransform() {
343    int w = mDisplayWidth;
344    int h = mDisplayHeight;
345    Transform TL, TP, R, S;
346    if (DisplayDevice::orientationToTransfrom(
347            mOrientation, w, h, &R) == NO_ERROR) {
348        dirtyRegion.set(bounds());
349
350        Rect viewport(mViewport);
351        Rect frame(mFrame);
352
353        if (!frame.isValid()) {
354            // the destination frame can be invalid if it has never been set,
355            // in that case we assume the whole display frame.
356            frame = Rect(w, h);
357        }
358
359        if (viewport.isEmpty()) {
360            // viewport can be invalid if it has never been set, in that case
361            // we assume the whole display size.
362            // it's also invalid to have an empty viewport, so we handle that
363            // case in the same way.
364            viewport = Rect(w, h);
365            if (R.getOrientation() & Transform::ROT_90) {
366                // viewport is always specified in the logical orientation
367                // of the display (ie: post-rotation).
368                swap(viewport.right, viewport.bottom);
369            }
370        }
371
372        float src_width  = viewport.width();
373        float src_height = viewport.height();
374        float dst_width  = frame.width();
375        float dst_height = frame.height();
376        if (src_width != dst_width || src_height != dst_height) {
377            float sx = dst_width  / src_width;
378            float sy = dst_height / src_height;
379            S.set(sx, 0, 0, sy);
380        }
381
382        float src_x = viewport.left;
383        float src_y = viewport.top;
384        float dst_x = frame.left;
385        float dst_y = frame.top;
386        TL.set(-src_x, -src_y);
387        TP.set(dst_x, dst_y);
388
389        // The viewport and frame are both in the logical orientation.
390        // Apply the logical translation, scale to physical size, apply the
391        // physical translation and finally rotate to the physical orientation.
392        mGlobalTransform = R * TP * S * TL;
393    }
394}
395