blob: 6a9cb3e031d73a1bb4c7e27f292f34bfe997097c [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 Agopianda8d0a52012-09-04 15:05:38 -070041#include "clz.h"
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -070042#include "DisplayDevice.h"
Mathias Agopian1f7bec62010-06-25 18:02:21 -070043#include "GLExtensions.h"
Mathias Agopianc7d14e22011-08-01 16:32:21 -070044#include "SurfaceFlinger.h"
Mathias Agopian921e6ac2012-07-23 23:11:29 -070045#include "LayerBase.h"
Mathias Agopian1f7bec62010-06-25 18:02:21 -070046
Mathias Agopiana4912602012-07-12 14:25:33 -070047// ----------------------------------------------------------------------------
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080048using namespace android;
Mathias Agopiana4912602012-07-12 14:25:33 -070049// ----------------------------------------------------------------------------
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080050
51static __attribute__((noinline))
52void checkGLErrors()
53{
Mathias Agopiancbb288b2009-09-07 16:32:45 -070054 do {
55 // there could be more than one error flag
56 GLenum error = glGetError();
57 if (error == GL_NO_ERROR)
58 break;
Steve Blocke6f43dd2012-01-06 19:20:56 +000059 ALOGE("GL error 0x%04x", int(error));
Mathias Agopiancbb288b2009-09-07 16:32:45 -070060 } while(true);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080061}
62
Mathias Agopiana4912602012-07-12 14:25:33 -070063// ----------------------------------------------------------------------------
64
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080065/*
66 * Initialize the display to the specified values.
67 *
68 */
69
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -070070DisplayDevice::DisplayDevice(
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080071 const sp<SurfaceFlinger>& flinger,
Mathias Agopian3ee454a2012-08-27 16:28:24 -070072 DisplayType type, const wp<IBinder>& displayToken,
Jamie Gennis1a4d8832012-08-02 20:11:05 -070073 const sp<ANativeWindow>& nativeWindow,
74 const sp<FramebufferSurface>& framebufferSurface,
Mathias Agopiana4912602012-07-12 14:25:33 -070075 EGLConfig config)
Mathias Agopian92a979a2012-08-02 18:32:23 -070076 : mFlinger(flinger),
Mathias Agopian3ee454a2012-08-27 16:28:24 -070077 mType(type), mHwcDisplayId(-1),
Jamie Gennis1a4d8832012-08-02 20:11:05 -070078 mNativeWindow(nativeWindow),
79 mFramebufferSurface(framebufferSurface),
Mathias Agopian92a979a2012-08-02 18:32:23 -070080 mDisplay(EGL_NO_DISPLAY),
81 mSurface(EGL_NO_SURFACE),
82 mContext(EGL_NO_CONTEXT),
Mathias Agopian92a979a2012-08-02 18:32:23 -070083 mDisplayWidth(), mDisplayHeight(), mFormat(),
84 mFlags(),
85 mPageFlipCount(),
Mathias Agopian92a979a2012-08-02 18:32:23 -070086 mSecureLayerVisible(false),
87 mScreenAcquired(false),
Mathias Agopianda8d0a52012-09-04 15:05:38 -070088 mLayerStack(0),
89 mOrientation()
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080090{
Mathias Agopiana4912602012-07-12 14:25:33 -070091 init(config);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080092}
93
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -070094DisplayDevice::~DisplayDevice() {
Mathias Agopian92a979a2012-08-02 18:32:23 -070095 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;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800103}
104
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700105int DisplayDevice::getWidth() const {
Mathias Agopiana4912602012-07-12 14:25:33 -0700106 return mDisplayWidth;
107}
108
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700109int DisplayDevice::getHeight() const {
Mathias Agopiana4912602012-07-12 14:25:33 -0700110 return mDisplayHeight;
111}
112
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700113PixelFormat DisplayDevice::getFormat() const {
Mathias Agopiana4912602012-07-12 14:25:33 -0700114 return mFormat;
115}
116
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700117EGLSurface DisplayDevice::getEGLSurface() const {
Mathias Agopiana4912602012-07-12 14:25:33 -0700118 return mSurface;
119}
120
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700121void DisplayDevice::init(EGLConfig config)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800122{
Mathias Agopiana4912602012-07-12 14:25:33 -0700123 ANativeWindow* const window = mNativeWindow.get();
124
Mathias Agopian61630912011-07-06 16:35:30 -0700125 int format;
Mathias Agopian61630912011-07-06 16:35:30 -0700126 window->query(window, NATIVE_WINDOW_FORMAT, &format);
Mathias Agopianb5dd9c02012-03-22 12:15:54 -0700127
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800128 /*
Mathias Agopiana4912602012-07-12 14:25:33 -0700129 * Create our display's surface
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800130 */
131
Mathias Agopiana4912602012-07-12 14:25:33 -0700132 EGLSurface surface;
133 EGLint w, h;
134 EGLDisplay display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
135 surface = eglCreateWindowSurface(display, config, window, NULL);
Mathias Agopian1b031492012-06-20 17:51:20 -0700136 eglQuerySurface(display, surface, EGL_WIDTH, &mDisplayWidth);
137 eglQuerySurface(display, surface, EGL_HEIGHT, &mDisplayHeight);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800138
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800139 mDisplay = display;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800140 mSurface = surface;
Mathias Agopiana4912602012-07-12 14:25:33 -0700141 mFormat = format;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700142 mPageFlipCount = 0;
Mathias Agopianda8d0a52012-09-04 15:05:38 -0700143 mViewport.makeInvalid();
144 mFrame.makeInvalid();
Mathias Agopian1f7bec62010-06-25 18:02:21 -0700145
Mathias Agopian5f20e2d2012-08-10 18:50:38 -0700146 // external displays are always considered enabled
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700147 mScreenAcquired = (mType >= DisplayDevice::NUM_DISPLAY_TYPES);
148
149 // get an h/w composer ID
150 mHwcDisplayId = mFlinger->allocateHwcDisplayId(mType);
Mathias Agopian5f20e2d2012-08-10 18:50:38 -0700151
Mathias Agopian98a121a2012-07-24 21:08:59 -0700152 // initialize the display orientation transform.
Mathias Agopian00e8c7a2012-09-04 19:30:46 -0700153 setProjection(DisplayState::eOrientationDefault, mViewport, mFrame);
Mathias Agopiana350ff92010-08-10 17:14:02 -0700154}
155
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700156uint32_t DisplayDevice::getPageFlipCount() const {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700157 return mPageFlipCount;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800158}
159
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700160status_t DisplayDevice::compositionComplete() const {
Mathias Agopiana4912602012-07-12 14:25:33 -0700161 if (mFramebufferSurface == NULL) {
162 return NO_ERROR;
163 }
164 return mFramebufferSurface->compositionComplete();
Mathias Agopian74faca22009-09-17 16:18:16 -0700165}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800166
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700167void DisplayDevice::flip(const Region& dirty) const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800168{
169 checkGLErrors();
170
171 EGLDisplay dpy = mDisplay;
172 EGLSurface surface = mSurface;
173
Mathias Agopian5e78e092009-06-11 17:19:54 -0700174#ifdef EGL_ANDROID_swap_rectangle
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700175 if (mFlags & SWAP_RECTANGLE) {
Mathias Agopianb8a55602009-06-26 19:06:36 -0700176 const Region newDirty(dirty.intersect(bounds()));
177 const Rect b(newDirty.getBounds());
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700178 eglSetSwapRectangleANDROID(dpy, surface,
179 b.left, b.top, b.width(), b.height());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800180 }
Mathias Agopian5e78e092009-06-11 17:19:54 -0700181#endif
182
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700183 mPageFlipCount++;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800184}
185
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700186uint32_t DisplayDevice::getFlags() const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800187{
188 return mFlags;
189}
190
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700191void DisplayDevice::dump(String8& res) const
Erik Gilling1d21a9c2010-12-01 16:38:01 -0800192{
Mathias Agopiana4912602012-07-12 14:25:33 -0700193 if (mFramebufferSurface != NULL) {
194 mFramebufferSurface->dump(res);
195 }
Erik Gilling1d21a9c2010-12-01 16:38:01 -0800196}
Mathias Agopian1b031492012-06-20 17:51:20 -0700197
Mathias Agopianda8d0a52012-09-04 15:05:38 -0700198EGLBoolean DisplayDevice::makeCurrent(EGLDisplay dpy,
199 const sp<const DisplayDevice>& hw, EGLContext ctx) {
200 EGLBoolean result = EGL_TRUE;
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700201 EGLSurface sur = eglGetCurrentSurface(EGL_DRAW);
Mathias Agopian42977342012-08-05 00:40:46 -0700202 if (sur != hw->mSurface) {
Mathias Agopianda8d0a52012-09-04 15:05:38 -0700203 result = eglMakeCurrent(dpy, hw->mSurface, hw->mSurface, ctx);
204 if (result == EGL_TRUE) {
205 GLsizei w = hw->mDisplayWidth;
206 GLsizei h = hw->mDisplayHeight;
207 glViewport(0, 0, w, h);
208 glMatrixMode(GL_PROJECTION);
209 glLoadIdentity();
210 // put the origin in the left-bottom corner
211 glOrthof(0, w, 0, h, 0, 1); // l=0, r=w ; b=0, t=h
212 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700213 }
Mathias Agopianda8d0a52012-09-04 15:05:38 -0700214 return result;
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700215}
216
Mathias Agopian1b031492012-06-20 17:51:20 -0700217// ----------------------------------------------------------------------------
218
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700219void DisplayDevice::setVisibleLayersSortedByZ(const Vector< sp<LayerBase> >& layers) {
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700220 mVisibleLayersSortedByZ = layers;
Mathias Agopianef7b9c72012-08-10 15:22:19 -0700221 mSecureLayerVisible = false;
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700222 size_t count = layers.size();
223 for (size_t i=0 ; i<count ; i++) {
224 if (layers[i]->isSecure()) {
225 mSecureLayerVisible = true;
226 }
227 }
228}
229
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700230const Vector< sp<LayerBase> >& DisplayDevice::getVisibleLayersSortedByZ() const {
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700231 return mVisibleLayersSortedByZ;
232}
233
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700234bool DisplayDevice::getSecureLayerVisible() const {
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700235 return mSecureLayerVisible;
236}
237
Mathias Agopiancd60f992012-08-16 16:28:27 -0700238Region DisplayDevice::getDirtyRegion(bool repaintEverything) const {
239 Region dirty;
Mathias Agopiancd60f992012-08-16 16:28:27 -0700240 if (repaintEverything) {
241 dirty.set(getBounds());
242 } else {
Mathias Agopianda8d0a52012-09-04 15:05:38 -0700243 const Transform& planeTransform(mGlobalTransform);
Mathias Agopiancd60f992012-08-16 16:28:27 -0700244 dirty = planeTransform.transform(this->dirtyRegion);
245 dirty.andSelf(getBounds());
246 }
247 return dirty;
248}
249
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700250// ----------------------------------------------------------------------------
251
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700252bool DisplayDevice::canDraw() const {
253 return mScreenAcquired;
254}
255
256void DisplayDevice::releaseScreen() const {
257 mScreenAcquired = false;
258}
259
260void DisplayDevice::acquireScreen() const {
261 mScreenAcquired = true;
262}
263
264bool DisplayDevice::isScreenAcquired() const {
265 return mScreenAcquired;
266}
267
268// ----------------------------------------------------------------------------
269
Mathias Agopian28947d72012-08-08 18:51:15 -0700270void DisplayDevice::setLayerStack(uint32_t stack) {
271 mLayerStack = stack;
272 dirtyRegion.set(bounds());
273}
274
275// ----------------------------------------------------------------------------
276
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700277status_t DisplayDevice::orientationToTransfrom(
Mathias Agopian1b031492012-06-20 17:51:20 -0700278 int orientation, int w, int h, Transform* tr)
279{
280 uint32_t flags = 0;
281 switch (orientation) {
Mathias Agopian3165cc22012-08-08 19:42:09 -0700282 case DisplayState::eOrientationDefault:
Mathias Agopian1b031492012-06-20 17:51:20 -0700283 flags = Transform::ROT_0;
284 break;
Mathias Agopian3165cc22012-08-08 19:42:09 -0700285 case DisplayState::eOrientation90:
Mathias Agopian1b031492012-06-20 17:51:20 -0700286 flags = Transform::ROT_90;
287 break;
Mathias Agopian3165cc22012-08-08 19:42:09 -0700288 case DisplayState::eOrientation180:
Mathias Agopian1b031492012-06-20 17:51:20 -0700289 flags = Transform::ROT_180;
290 break;
Mathias Agopian3165cc22012-08-08 19:42:09 -0700291 case DisplayState::eOrientation270:
Mathias Agopian1b031492012-06-20 17:51:20 -0700292 flags = Transform::ROT_270;
293 break;
294 default:
295 return BAD_VALUE;
296 }
297 tr->set(flags, w, h);
298 return NO_ERROR;
299}
300
Mathias Agopian00e8c7a2012-09-04 19:30:46 -0700301void DisplayDevice::setProjection(int orientation,
302 const Rect& viewport, const Rect& frame) {
Mathias Agopianda8d0a52012-09-04 15:05:38 -0700303 mOrientation = orientation;
Mathias Agopian00e8c7a2012-09-04 19:30:46 -0700304 mViewport = viewport;
305 mFrame = frame;
Mathias Agopianda8d0a52012-09-04 15:05:38 -0700306 updateGeometryTransform();
307}
308
Mathias Agopianda8d0a52012-09-04 15:05:38 -0700309void DisplayDevice::updateGeometryTransform() {
Mathias Agopian98a121a2012-07-24 21:08:59 -0700310 int w = mDisplayWidth;
311 int h = mDisplayHeight;
Jesse Hall6360ec42012-09-12 13:49:10 -0700312 Transform T, R, S;
Mathias Agopianda8d0a52012-09-04 15:05:38 -0700313 if (DisplayDevice::orientationToTransfrom(
314 mOrientation, w, h, &R) == NO_ERROR) {
315 dirtyRegion.set(bounds());
Mathias Agopian1b031492012-06-20 17:51:20 -0700316
Mathias Agopianda8d0a52012-09-04 15:05:38 -0700317 Rect viewport(mViewport);
318 Rect frame(mFrame);
319
320 if (!frame.isValid()) {
321 // the destination frame can be invalid if it has never been set,
322 // in that case we assume the whole display frame.
323 frame = Rect(w, h);
324 }
325
326 if (viewport.isEmpty()) {
327 // viewport can be invalid if it has never been set, in that case
328 // we assume the whole display size.
329 // it's also invalid to have an empty viewport, so we handle that
330 // case in the same way.
331 viewport = Rect(w, h);
332 if (R.getOrientation() & Transform::ROT_90) {
333 // viewport is always specified in the logical orientation
334 // of the display (ie: post-rotation).
335 swap(viewport.right, viewport.bottom);
336 }
337 }
338
339 float src_width = viewport.width();
340 float src_height = viewport.height();
341 float dst_width = frame.width();
342 float dst_height = frame.height();
Jesse Hall6360ec42012-09-12 13:49:10 -0700343 if (src_width != dst_width || src_height != dst_height) {
Mathias Agopianda8d0a52012-09-04 15:05:38 -0700344 float sx = dst_width / src_width;
345 float sy = dst_height / src_height;
346 S.set(sx, 0, 0, sy);
347 }
Jesse Hall6360ec42012-09-12 13:49:10 -0700348
Mathias Agopianda8d0a52012-09-04 15:05:38 -0700349 float src_x = viewport.left;
350 float src_y = viewport.top;
351 float dst_x = frame.left;
352 float dst_y = frame.top;
353 float tx = dst_x - src_x;
354 float ty = dst_y - src_y;
Jesse Hall6360ec42012-09-12 13:49:10 -0700355 T.set(tx, ty);
Mathias Agopianda8d0a52012-09-04 15:05:38 -0700356
Jesse Hall6360ec42012-09-12 13:49:10 -0700357 // The viewport and frame are both in the logical orientation, so the
358 // translation is also in that space. So translation must be applied
359 // before rotating from logical to physical orientation.
360 mGlobalTransform = S * R * T;
Mathias Agopian1b031492012-06-20 17:51:20 -0700361 }
Mathias Agopian1b031492012-06-20 17:51:20 -0700362}