2 * Copyright (C) 2007 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
22 #include <cutils/properties.h>
24 #include <utils/RefBase.h>
25 #include <utils/Log.h>
27 #include <ui/DisplayInfo.h>
28 #include <ui/PixelFormat.h>
30 #include <gui/SurfaceTextureClient.h>
34 #include <EGL/eglext.h>
36 #include <hardware/gralloc.h>
38 #include "DisplayHardware/FramebufferSurface.h"
39 #include "DisplayHardware/HWComposer.h"
41 #include "DisplayDevice.h"
42 #include "GLExtensions.h"
43 #include "SurfaceFlinger.h"
44 #include "LayerBase.h"
46 // ----------------------------------------------------------------------------
47 using namespace android;
48 // ----------------------------------------------------------------------------
50 static __attribute__((noinline))
54 // there could be more than one error flag
55 GLenum error = glGetError();
56 if (error == GL_NO_ERROR)
58 ALOGE("GL error 0x%04x", int(error));
62 // ----------------------------------------------------------------------------
65 * Initialize the display to the specified values.
69 DisplayDevice::DisplayDevice(
70 const sp<SurfaceFlinger>& flinger,
72 const sp<ANativeWindow>& nativeWindow,
73 const sp<FramebufferSurface>& framebufferSurface,
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(),
85 mSecureLayerVisible(false),
86 mScreenAcquired(false),
93 DisplayDevice::~DisplayDevice() {
94 if (mSurface != EGL_NO_SURFACE) {
95 eglDestroySurface(mDisplay, mSurface);
96 mSurface = EGL_NO_SURFACE;
100 bool DisplayDevice::isValid() const {
101 return mFlinger != NULL;
104 int DisplayDevice::getWidth() const {
105 return mDisplayWidth;
108 int DisplayDevice::getHeight() const {
109 return mDisplayHeight;
112 PixelFormat DisplayDevice::getFormat() const {
116 EGLSurface DisplayDevice::getEGLSurface() const {
120 void DisplayDevice::init(EGLConfig config)
122 ANativeWindow* const window = mNativeWindow.get();
125 window->query(window, NATIVE_WINDOW_FORMAT, &format);
128 * Create our display's surface
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);
143 // external displays are always considered enabled
144 mScreenAcquired = mId >= DisplayDevice::DISPLAY_ID_COUNT;
146 // initialize the display orientation transform.
147 DisplayDevice::setOrientation(DisplayState::eOrientationDefault);
150 uint32_t DisplayDevice::getPageFlipCount() const {
151 return mPageFlipCount;
154 status_t DisplayDevice::compositionComplete() const {
155 if (mFramebufferSurface == NULL) {
158 return mFramebufferSurface->compositionComplete();
161 void DisplayDevice::flip(const Region& dirty) const
165 EGLDisplay dpy = mDisplay;
166 EGLSurface surface = mSurface;
168 #ifdef EGL_ANDROID_swap_rectangle
169 if (mFlags & SWAP_RECTANGLE) {
170 const Region newDirty(dirty.intersect(bounds()));
171 const Rect b(newDirty.getBounds());
172 eglSetSwapRectangleANDROID(dpy, surface,
173 b.left, b.top, b.width(), b.height());
180 uint32_t DisplayDevice::getFlags() const
185 void DisplayDevice::dump(String8& res) const
187 if (mFramebufferSurface != NULL) {
188 mFramebufferSurface->dump(res);
192 void DisplayDevice::makeCurrent(const sp<const DisplayDevice>& hw, EGLContext ctx) {
193 EGLSurface sur = eglGetCurrentSurface(EGL_DRAW);
194 if (sur != hw->mSurface) {
195 EGLDisplay dpy = eglGetCurrentDisplay();
196 eglMakeCurrent(dpy, hw->mSurface, hw->mSurface, ctx);
200 // ----------------------------------------------------------------------------
202 void DisplayDevice::setVisibleLayersSortedByZ(const Vector< sp<LayerBase> >& layers) {
203 mVisibleLayersSortedByZ = layers;
204 mSecureLayerVisible = false;
205 size_t count = layers.size();
206 for (size_t i=0 ; i<count ; i++) {
207 if (layers[i]->isSecure()) {
208 mSecureLayerVisible = true;
213 Vector< sp<LayerBase> > DisplayDevice::getVisibleLayersSortedByZ() const {
214 return mVisibleLayersSortedByZ;
217 bool DisplayDevice::getSecureLayerVisible() const {
218 return mSecureLayerVisible;
221 Region DisplayDevice::getDirtyRegion(bool repaintEverything) const {
223 const Transform& planeTransform(mGlobalTransform);
224 if (repaintEverything) {
225 dirty.set(getBounds());
227 dirty = planeTransform.transform(this->dirtyRegion);
228 dirty.andSelf(getBounds());
233 // ----------------------------------------------------------------------------
235 bool DisplayDevice::canDraw() const {
236 return mScreenAcquired;
239 void DisplayDevice::releaseScreen() const {
240 mScreenAcquired = false;
243 void DisplayDevice::acquireScreen() const {
244 mScreenAcquired = true;
247 bool DisplayDevice::isScreenAcquired() const {
248 return mScreenAcquired;
251 // ----------------------------------------------------------------------------
253 void DisplayDevice::setLayerStack(uint32_t stack) {
255 dirtyRegion.set(bounds());
258 // ----------------------------------------------------------------------------
260 status_t DisplayDevice::orientationToTransfrom(
261 int orientation, int w, int h, Transform* tr)
264 switch (orientation) {
265 case DisplayState::eOrientationDefault:
266 flags = Transform::ROT_0;
268 case DisplayState::eOrientation90:
269 flags = Transform::ROT_90;
271 case DisplayState::eOrientation180:
272 flags = Transform::ROT_180;
274 case DisplayState::eOrientation270:
275 flags = Transform::ROT_270;
280 tr->set(flags, w, h);
284 status_t DisplayDevice::setOrientation(int orientation) {
285 int w = mDisplayWidth;
286 int h = mDisplayHeight;
288 DisplayDevice::orientationToTransfrom(
289 orientation, w, h, &mGlobalTransform);
290 if (orientation & DisplayState::eOrientationSwapMask) {
295 mOrientation = orientation;
296 dirtyRegion.set(bounds());