blob: 16e55471abfe0fdc01df81a131c9d1a4c8056168 [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 Agopian3ee454a2012-08-27 16:28:24 -070071 DisplayType type, const wp<IBinder>& displayToken,
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),
Mathias Agopian3ee454a2012-08-27 16:28:24 -070076 mType(type), mHwcDisplayId(-1),
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 Agopian5f20e2d2012-08-10 18:50:38 -0700143 // external displays are always considered enabled
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700144 mScreenAcquired = (mType >= DisplayDevice::NUM_DISPLAY_TYPES);
145
146 // get an h/w composer ID
147 mHwcDisplayId = mFlinger->allocateHwcDisplayId(mType);
Mathias Agopian5f20e2d2012-08-10 18:50:38 -0700148
Mathias Agopian98a121a2012-07-24 21:08:59 -0700149 // initialize the display orientation transform.
Mathias Agopian3165cc22012-08-08 19:42:09 -0700150 DisplayDevice::setOrientation(DisplayState::eOrientationDefault);
Mathias Agopiana350ff92010-08-10 17:14:02 -0700151}
152
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700153uint32_t DisplayDevice::getPageFlipCount() const {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700154 return mPageFlipCount;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800155}
156
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700157status_t DisplayDevice::compositionComplete() const {
Mathias Agopiana4912602012-07-12 14:25:33 -0700158 if (mFramebufferSurface == NULL) {
159 return NO_ERROR;
160 }
161 return mFramebufferSurface->compositionComplete();
Mathias Agopian74faca22009-09-17 16:18:16 -0700162}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800163
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700164void DisplayDevice::flip(const Region& dirty) const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800165{
166 checkGLErrors();
167
168 EGLDisplay dpy = mDisplay;
169 EGLSurface surface = mSurface;
170
Mathias Agopian5e78e092009-06-11 17:19:54 -0700171#ifdef EGL_ANDROID_swap_rectangle
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700172 if (mFlags & SWAP_RECTANGLE) {
Mathias Agopianb8a55602009-06-26 19:06:36 -0700173 const Region newDirty(dirty.intersect(bounds()));
174 const Rect b(newDirty.getBounds());
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700175 eglSetSwapRectangleANDROID(dpy, surface,
176 b.left, b.top, b.width(), b.height());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800177 }
Mathias Agopian5e78e092009-06-11 17:19:54 -0700178#endif
179
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700180 mPageFlipCount++;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800181}
182
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700183uint32_t DisplayDevice::getFlags() const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800184{
185 return mFlags;
186}
187
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700188void DisplayDevice::dump(String8& res) const
Erik Gilling1d21a9c2010-12-01 16:38:01 -0800189{
Mathias Agopiana4912602012-07-12 14:25:33 -0700190 if (mFramebufferSurface != NULL) {
191 mFramebufferSurface->dump(res);
192 }
Erik Gilling1d21a9c2010-12-01 16:38:01 -0800193}
Mathias Agopian1b031492012-06-20 17:51:20 -0700194
Mathias Agopian42977342012-08-05 00:40:46 -0700195void DisplayDevice::makeCurrent(const sp<const DisplayDevice>& hw, EGLContext ctx) {
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700196 EGLSurface sur = eglGetCurrentSurface(EGL_DRAW);
Mathias Agopian42977342012-08-05 00:40:46 -0700197 if (sur != hw->mSurface) {
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700198 EGLDisplay dpy = eglGetCurrentDisplay();
Mathias Agopian42977342012-08-05 00:40:46 -0700199 eglMakeCurrent(dpy, hw->mSurface, hw->mSurface, ctx);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700200 }
201}
202
Mathias Agopian1b031492012-06-20 17:51:20 -0700203// ----------------------------------------------------------------------------
204
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700205void DisplayDevice::setVisibleLayersSortedByZ(const Vector< sp<LayerBase> >& layers) {
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700206 mVisibleLayersSortedByZ = layers;
Mathias Agopianef7b9c72012-08-10 15:22:19 -0700207 mSecureLayerVisible = false;
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700208 size_t count = layers.size();
209 for (size_t i=0 ; i<count ; i++) {
210 if (layers[i]->isSecure()) {
211 mSecureLayerVisible = true;
212 }
213 }
214}
215
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700216const Vector< sp<LayerBase> >& DisplayDevice::getVisibleLayersSortedByZ() const {
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700217 return mVisibleLayersSortedByZ;
218}
219
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700220bool DisplayDevice::getSecureLayerVisible() const {
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700221 return mSecureLayerVisible;
222}
223
Mathias Agopiancd60f992012-08-16 16:28:27 -0700224Region DisplayDevice::getDirtyRegion(bool repaintEverything) const {
225 Region dirty;
226 const Transform& planeTransform(mGlobalTransform);
227 if (repaintEverything) {
228 dirty.set(getBounds());
229 } else {
230 dirty = planeTransform.transform(this->dirtyRegion);
231 dirty.andSelf(getBounds());
232 }
233 return dirty;
234}
235
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700236// ----------------------------------------------------------------------------
237
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700238bool DisplayDevice::canDraw() const {
239 return mScreenAcquired;
240}
241
242void DisplayDevice::releaseScreen() const {
243 mScreenAcquired = false;
244}
245
246void DisplayDevice::acquireScreen() const {
247 mScreenAcquired = true;
248}
249
250bool DisplayDevice::isScreenAcquired() const {
251 return mScreenAcquired;
252}
253
254// ----------------------------------------------------------------------------
255
Mathias Agopian28947d72012-08-08 18:51:15 -0700256void DisplayDevice::setLayerStack(uint32_t stack) {
257 mLayerStack = stack;
258 dirtyRegion.set(bounds());
259}
260
261// ----------------------------------------------------------------------------
262
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700263status_t DisplayDevice::orientationToTransfrom(
Mathias Agopian1b031492012-06-20 17:51:20 -0700264 int orientation, int w, int h, Transform* tr)
265{
266 uint32_t flags = 0;
267 switch (orientation) {
Mathias Agopian3165cc22012-08-08 19:42:09 -0700268 case DisplayState::eOrientationDefault:
Mathias Agopian1b031492012-06-20 17:51:20 -0700269 flags = Transform::ROT_0;
270 break;
Mathias Agopian3165cc22012-08-08 19:42:09 -0700271 case DisplayState::eOrientation90:
Mathias Agopian1b031492012-06-20 17:51:20 -0700272 flags = Transform::ROT_90;
273 break;
Mathias Agopian3165cc22012-08-08 19:42:09 -0700274 case DisplayState::eOrientation180:
Mathias Agopian1b031492012-06-20 17:51:20 -0700275 flags = Transform::ROT_180;
276 break;
Mathias Agopian3165cc22012-08-08 19:42:09 -0700277 case DisplayState::eOrientation270:
Mathias Agopian1b031492012-06-20 17:51:20 -0700278 flags = Transform::ROT_270;
279 break;
280 default:
281 return BAD_VALUE;
282 }
283 tr->set(flags, w, h);
284 return NO_ERROR;
285}
286
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700287status_t DisplayDevice::setOrientation(int orientation) {
Mathias Agopian98a121a2012-07-24 21:08:59 -0700288 int w = mDisplayWidth;
289 int h = mDisplayHeight;
Mathias Agopian1b031492012-06-20 17:51:20 -0700290
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700291 DisplayDevice::orientationToTransfrom(
Mathias Agopian98a121a2012-07-24 21:08:59 -0700292 orientation, w, h, &mGlobalTransform);
Mathias Agopian3165cc22012-08-08 19:42:09 -0700293 if (orientation & DisplayState::eOrientationSwapMask) {
Mathias Agopian98a121a2012-07-24 21:08:59 -0700294 int tmp = w;
295 w = h;
296 h = tmp;
Mathias Agopian1b031492012-06-20 17:51:20 -0700297 }
Mathias Agopian1b031492012-06-20 17:51:20 -0700298 mOrientation = orientation;
Mathias Agopian92a979a2012-08-02 18:32:23 -0700299 dirtyRegion.set(bounds());
Mathias Agopian1b031492012-06-20 17:51:20 -0700300 return NO_ERROR;
301}