blob: 4cae6926f55d5585e2ce9c1453d8bf01ad012347 [file] [log] [blame]
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001/*
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
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080017#include <stdlib.h>
18#include <stdio.h>
19#include <string.h>
20#include <math.h>
21
22#include <cutils/properties.h>
23
Mathias Agopian076b1cc2009-04-10 14:24:30 -070024#include <utils/RefBase.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080025#include <utils/Log.h>
26
Mathias Agopianc666cae2012-07-25 18:56:13 -070027#include <ui/DisplayInfo.h>
Mathias Agopian076b1cc2009-04-10 14:24:30 -070028#include <ui/PixelFormat.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080029
Jamie Gennis1a4d8832012-08-02 20:11:05 -070030#include <gui/SurfaceTextureClient.h>
31
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080032#include <GLES/gl.h>
Mathias Agopian076b1cc2009-04-10 14:24:30 -070033#include <EGL/egl.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080034#include <EGL/eglext.h>
35
Mathias Agopian076b1cc2009-04-10 14:24:30 -070036#include <hardware/gralloc.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080037
Mathias Agopian1b031492012-06-20 17:51:20 -070038#include "DisplayHardware/FramebufferSurface.h"
Mathias Agopian1b031492012-06-20 17:51:20 -070039#include "DisplayHardware/HWComposer.h"
40
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -070041#include "DisplayDevice.h"
Mathias Agopian1f7bec62010-06-25 18:02:21 -070042#include "GLExtensions.h"
Mathias Agopianc7d14e22011-08-01 16:32:21 -070043#include "SurfaceFlinger.h"
Mathias Agopian921e6ac2012-07-23 23:11:29 -070044#include "LayerBase.h"
Mathias Agopian1f7bec62010-06-25 18:02:21 -070045
Mathias Agopiana4912602012-07-12 14:25:33 -070046// ----------------------------------------------------------------------------
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080047using namespace android;
Mathias Agopiana4912602012-07-12 14:25:33 -070048// ----------------------------------------------------------------------------
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080049
50static __attribute__((noinline))
51void checkGLErrors()
52{
Mathias Agopiancbb288b2009-09-07 16:32:45 -070053 do {
54 // there could be more than one error flag
55 GLenum error = glGetError();
56 if (error == GL_NO_ERROR)
57 break;
Steve Blocke6f43dd2012-01-06 19:20:56 +000058 ALOGE("GL error 0x%04x", int(error));
Mathias Agopiancbb288b2009-09-07 16:32:45 -070059 } while(true);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080060}
61
Mathias Agopiana4912602012-07-12 14:25:33 -070062// ----------------------------------------------------------------------------
63
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080064/*
65 * Initialize the display to the specified values.
66 *
67 */
68
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -070069DisplayDevice::DisplayDevice(
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080070 const sp<SurfaceFlinger>& flinger,
Mathias Agopiana4912602012-07-12 14:25:33 -070071 int display,
Jamie Gennis1a4d8832012-08-02 20:11:05 -070072 const sp<ANativeWindow>& nativeWindow,
73 const sp<FramebufferSurface>& framebufferSurface,
Mathias Agopiana4912602012-07-12 14:25:33 -070074 EGLConfig config)
Mathias Agopian92a979a2012-08-02 18:32:23 -070075 : mFlinger(flinger),
76 mId(display),
Jamie Gennis1a4d8832012-08-02 20:11:05 -070077 mNativeWindow(nativeWindow),
78 mFramebufferSurface(framebufferSurface),
Mathias Agopian92a979a2012-08-02 18:32:23 -070079 mDisplay(EGL_NO_DISPLAY),
80 mSurface(EGL_NO_SURFACE),
81 mContext(EGL_NO_CONTEXT),
Mathias Agopian92a979a2012-08-02 18:32:23 -070082 mDisplayWidth(), mDisplayHeight(), mFormat(),
83 mFlags(),
84 mPageFlipCount(),
Mathias Agopian92a979a2012-08-02 18:32:23 -070085 mSecureLayerVisible(false),
86 mScreenAcquired(false),
87 mOrientation(),
88 mLayerStack(0)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080089{
Mathias Agopiana4912602012-07-12 14:25:33 -070090 init(config);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080091}
92
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -070093DisplayDevice::~DisplayDevice() {
Mathias Agopian92a979a2012-08-02 18:32:23 -070094 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;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800102}
103
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700104int DisplayDevice::getWidth() const {
Mathias Agopiana4912602012-07-12 14:25:33 -0700105 return mDisplayWidth;
106}
107
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700108int DisplayDevice::getHeight() const {
Mathias Agopiana4912602012-07-12 14:25:33 -0700109 return mDisplayHeight;
110}
111
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700112PixelFormat DisplayDevice::getFormat() const {
Mathias Agopiana4912602012-07-12 14:25:33 -0700113 return mFormat;
114}
115
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700116EGLSurface DisplayDevice::getEGLSurface() const {
Mathias Agopiana4912602012-07-12 14:25:33 -0700117 return mSurface;
118}
119
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700120void DisplayDevice::init(EGLConfig config)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800121{
Mathias Agopiana4912602012-07-12 14:25:33 -0700122 ANativeWindow* const window = mNativeWindow.get();
123
Mathias Agopian61630912011-07-06 16:35:30 -0700124 int format;
Mathias Agopian61630912011-07-06 16:35:30 -0700125 window->query(window, NATIVE_WINDOW_FORMAT, &format);
Mathias Agopianb5dd9c02012-03-22 12:15:54 -0700126
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800127 /*
Mathias Agopiana4912602012-07-12 14:25:33 -0700128 * Create our display's surface
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800129 */
130
Mathias Agopiana4912602012-07-12 14:25:33 -0700131 EGLSurface surface;
132 EGLint w, h;
133 EGLDisplay display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
134 surface = eglCreateWindowSurface(display, config, window, NULL);
Mathias Agopian1b031492012-06-20 17:51:20 -0700135 eglQuerySurface(display, surface, EGL_WIDTH, &mDisplayWidth);
136 eglQuerySurface(display, surface, EGL_HEIGHT, &mDisplayHeight);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800137
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800138 mDisplay = display;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800139 mSurface = surface;
Mathias Agopiana4912602012-07-12 14:25:33 -0700140 mFormat = format;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700141 mPageFlipCount = 0;
Mathias Agopian1f7bec62010-06-25 18:02:21 -0700142
Mathias Agopian98a121a2012-07-24 21:08:59 -0700143 // initialize the display orientation transform.
Mathias Agopian3165cc22012-08-08 19:42:09 -0700144 DisplayDevice::setOrientation(DisplayState::eOrientationDefault);
Mathias Agopiana350ff92010-08-10 17:14:02 -0700145}
146
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700147uint32_t DisplayDevice::getPageFlipCount() const {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700148 return mPageFlipCount;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800149}
150
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700151status_t DisplayDevice::compositionComplete() const {
Mathias Agopiana4912602012-07-12 14:25:33 -0700152 if (mFramebufferSurface == NULL) {
153 return NO_ERROR;
154 }
155 return mFramebufferSurface->compositionComplete();
Mathias Agopian74faca22009-09-17 16:18:16 -0700156}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800157
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700158void DisplayDevice::flip(const Region& dirty) const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800159{
160 checkGLErrors();
161
162 EGLDisplay dpy = mDisplay;
163 EGLSurface surface = mSurface;
164
Mathias Agopian5e78e092009-06-11 17:19:54 -0700165#ifdef EGL_ANDROID_swap_rectangle
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700166 if (mFlags & SWAP_RECTANGLE) {
Mathias Agopianb8a55602009-06-26 19:06:36 -0700167 const Region newDirty(dirty.intersect(bounds()));
168 const Rect b(newDirty.getBounds());
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700169 eglSetSwapRectangleANDROID(dpy, surface,
170 b.left, b.top, b.width(), b.height());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800171 }
Mathias Agopian5e78e092009-06-11 17:19:54 -0700172#endif
173
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700174 mPageFlipCount++;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800175}
176
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700177uint32_t DisplayDevice::getFlags() const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800178{
179 return mFlags;
180}
181
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700182void DisplayDevice::dump(String8& res) const
Erik Gilling1d21a9c2010-12-01 16:38:01 -0800183{
Mathias Agopiana4912602012-07-12 14:25:33 -0700184 if (mFramebufferSurface != NULL) {
185 mFramebufferSurface->dump(res);
186 }
Erik Gilling1d21a9c2010-12-01 16:38:01 -0800187}
Mathias Agopian1b031492012-06-20 17:51:20 -0700188
Mathias Agopian42977342012-08-05 00:40:46 -0700189void DisplayDevice::makeCurrent(const sp<const DisplayDevice>& hw, EGLContext ctx) {
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700190 EGLSurface sur = eglGetCurrentSurface(EGL_DRAW);
Mathias Agopian42977342012-08-05 00:40:46 -0700191 if (sur != hw->mSurface) {
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700192 EGLDisplay dpy = eglGetCurrentDisplay();
Mathias Agopian42977342012-08-05 00:40:46 -0700193 eglMakeCurrent(dpy, hw->mSurface, hw->mSurface, ctx);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700194 }
195}
196
Mathias Agopian1b031492012-06-20 17:51:20 -0700197// ----------------------------------------------------------------------------
198
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700199void DisplayDevice::setVisibleLayersSortedByZ(const Vector< sp<LayerBase> >& layers) {
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700200 mVisibleLayersSortedByZ = layers;
Mathias Agopianef7b9c72012-08-10 15:22:19 -0700201 mSecureLayerVisible = false;
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700202 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
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700210Vector< sp<LayerBase> > DisplayDevice::getVisibleLayersSortedByZ() const {
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700211 return mVisibleLayersSortedByZ;
212}
213
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700214bool DisplayDevice::getSecureLayerVisible() const {
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700215 return mSecureLayerVisible;
216}
217
218// ----------------------------------------------------------------------------
219
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700220bool 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
Mathias Agopian28947d72012-08-08 18:51:15 -0700238void DisplayDevice::setLayerStack(uint32_t stack) {
239 mLayerStack = stack;
240 dirtyRegion.set(bounds());
241}
242
243// ----------------------------------------------------------------------------
244
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700245status_t DisplayDevice::orientationToTransfrom(
Mathias Agopian1b031492012-06-20 17:51:20 -0700246 int orientation, int w, int h, Transform* tr)
247{
248 uint32_t flags = 0;
249 switch (orientation) {
Mathias Agopian3165cc22012-08-08 19:42:09 -0700250 case DisplayState::eOrientationDefault:
Mathias Agopian1b031492012-06-20 17:51:20 -0700251 flags = Transform::ROT_0;
252 break;
Mathias Agopian3165cc22012-08-08 19:42:09 -0700253 case DisplayState::eOrientation90:
Mathias Agopian1b031492012-06-20 17:51:20 -0700254 flags = Transform::ROT_90;
255 break;
Mathias Agopian3165cc22012-08-08 19:42:09 -0700256 case DisplayState::eOrientation180:
Mathias Agopian1b031492012-06-20 17:51:20 -0700257 flags = Transform::ROT_180;
258 break;
Mathias Agopian3165cc22012-08-08 19:42:09 -0700259 case DisplayState::eOrientation270:
Mathias Agopian1b031492012-06-20 17:51:20 -0700260 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
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700269status_t DisplayDevice::setOrientation(int orientation) {
Mathias Agopian98a121a2012-07-24 21:08:59 -0700270 int w = mDisplayWidth;
271 int h = mDisplayHeight;
Mathias Agopian1b031492012-06-20 17:51:20 -0700272
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700273 DisplayDevice::orientationToTransfrom(
Mathias Agopian98a121a2012-07-24 21:08:59 -0700274 orientation, w, h, &mGlobalTransform);
Mathias Agopian3165cc22012-08-08 19:42:09 -0700275 if (orientation & DisplayState::eOrientationSwapMask) {
Mathias Agopian98a121a2012-07-24 21:08:59 -0700276 int tmp = w;
277 w = h;
278 h = tmp;
Mathias Agopian1b031492012-06-20 17:51:20 -0700279 }
Mathias Agopian1b031492012-06-20 17:51:20 -0700280 mOrientation = orientation;
Mathias Agopian92a979a2012-08-02 18:32:23 -0700281 dirtyRegion.set(bounds());
Mathias Agopian1b031492012-06-20 17:51:20 -0700282 return NO_ERROR;
283}