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"
42 #include "DisplayDevice.h"
43 #include "GLExtensions.h"
44 #include "SurfaceFlinger.h"
45 #include "LayerBase.h"
47 // ----------------------------------------------------------------------------
48 using namespace android;
49 // ----------------------------------------------------------------------------
51 static __attribute__((noinline))
55 // there could be more than one error flag
56 GLenum error = glGetError();
57 if (error == GL_NO_ERROR)
59 ALOGE("GL error 0x%04x", int(error));
63 // ----------------------------------------------------------------------------
66 * Initialize the display to the specified values.
70 DisplayDevice::DisplayDevice(
71 const sp<SurfaceFlinger>& flinger,
72 DisplayType type, const wp<IBinder>& displayToken,
73 const sp<ANativeWindow>& nativeWindow,
74 const sp<FramebufferSurface>& framebufferSurface,
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(),
86 mSecureLayerVisible(false),
87 mScreenAcquired(false),
94 DisplayDevice::~DisplayDevice() {
95 if (mSurface != EGL_NO_SURFACE) {
96 eglDestroySurface(mDisplay, mSurface);
97 mSurface = EGL_NO_SURFACE;
101 bool DisplayDevice::isValid() const {
102 return mFlinger != NULL;
105 int DisplayDevice::getWidth() const {
106 return mDisplayWidth;
109 int DisplayDevice::getHeight() const {
110 return mDisplayHeight;
113 PixelFormat DisplayDevice::getFormat() const {
117 EGLSurface DisplayDevice::getEGLSurface() const {
121 void DisplayDevice::init(EGLConfig config)
123 ANativeWindow* const window = mNativeWindow.get();
126 window->query(window, NATIVE_WINDOW_FORMAT, &format);
129 * Create our display's surface
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);
143 mViewport.makeInvalid();
144 mFrame.makeInvalid();
146 // external displays are always considered enabled
147 mScreenAcquired = (mType >= DisplayDevice::NUM_DISPLAY_TYPES);
149 // get an h/w composer ID
150 mHwcDisplayId = mFlinger->allocateHwcDisplayId(mType);
152 // Name the display. The name will be replaced shortly if the display
153 // was created with createDisplay().
155 case DISPLAY_PRIMARY:
156 mDisplayName = "Built-in Screen";
158 case DISPLAY_EXTERNAL:
159 mDisplayName = "HDMI Screen";
162 mDisplayName = "Virtual Screen"; // e.g. Overlay #n
166 // initialize the display orientation transform.
167 setProjection(DisplayState::eOrientationDefault, mViewport, mFrame);
170 uint32_t DisplayDevice::getPageFlipCount() const {
171 return mPageFlipCount;
174 status_t DisplayDevice::compositionComplete() const {
175 if (mFramebufferSurface == NULL) {
178 return mFramebufferSurface->compositionComplete();
181 void DisplayDevice::flip(const Region& dirty) const
185 EGLDisplay dpy = mDisplay;
186 EGLSurface surface = mSurface;
188 #ifdef EGL_ANDROID_swap_rectangle
189 if (mFlags & SWAP_RECTANGLE) {
190 const Region newDirty(dirty.intersect(bounds()));
191 const Rect b(newDirty.getBounds());
192 eglSetSwapRectangleANDROID(dpy, surface,
193 b.left, b.top, b.width(), b.height());
200 void DisplayDevice::swapBuffers(HWComposer& hwc) const {
201 if (hwc.initCheck() != NO_ERROR) {
202 // no HWC, we call eglSwapBuffers()
203 eglSwapBuffers(mDisplay, mSurface);
205 // We have a valid HWC, but not all displays can use it, in particular
206 // the virtual displays are on their own.
207 // TODO: HWC 1.2 will allow virtual displays
208 if (mType >= DisplayDevice::DISPLAY_VIRTUAL) {
209 // always call eglSwapBuffers() for virtual displays
210 eglSwapBuffers(mDisplay, mSurface);
211 } else if (hwc.supportsFramebufferTarget()) {
212 // as of hwc 1.1 we always call eglSwapBuffers if we have some
214 if (hwc.hasGlesComposition(mType)) {
215 eglSwapBuffers(mDisplay, mSurface);
218 // HWC doesn't have the framebuffer target, we don't call
219 // eglSwapBuffers(), since this is handled by HWComposer::commit().
224 void DisplayDevice::onSwapBuffersCompleted(HWComposer& hwc) const {
225 if (hwc.initCheck() == NO_ERROR) {
226 if (hwc.supportsFramebufferTarget()) {
227 int fd = hwc.getAndResetReleaseFenceFd(mType);
228 mFramebufferSurface->setReleaseFenceFd(fd);
233 uint32_t DisplayDevice::getFlags() const
238 void DisplayDevice::dump(String8& res) const
240 if (mFramebufferSurface != NULL) {
241 mFramebufferSurface->dump(res);
245 EGLBoolean DisplayDevice::makeCurrent(EGLDisplay dpy,
246 const sp<const DisplayDevice>& hw, EGLContext ctx) {
247 EGLBoolean result = EGL_TRUE;
248 EGLSurface sur = eglGetCurrentSurface(EGL_DRAW);
249 if (sur != hw->mSurface) {
250 result = eglMakeCurrent(dpy, hw->mSurface, hw->mSurface, ctx);
251 if (result == EGL_TRUE) {
252 GLsizei w = hw->mDisplayWidth;
253 GLsizei h = hw->mDisplayHeight;
254 glViewport(0, 0, w, h);
255 glMatrixMode(GL_PROJECTION);
257 // put the origin in the left-bottom corner
258 glOrthof(0, w, 0, h, 0, 1); // l=0, r=w ; b=0, t=h
264 // ----------------------------------------------------------------------------
266 void DisplayDevice::setVisibleLayersSortedByZ(const Vector< sp<LayerBase> >& layers) {
267 mVisibleLayersSortedByZ = layers;
268 mSecureLayerVisible = false;
269 size_t count = layers.size();
270 for (size_t i=0 ; i<count ; i++) {
271 if (layers[i]->isSecure()) {
272 mSecureLayerVisible = true;
277 const Vector< sp<LayerBase> >& DisplayDevice::getVisibleLayersSortedByZ() const {
278 return mVisibleLayersSortedByZ;
281 bool DisplayDevice::getSecureLayerVisible() const {
282 return mSecureLayerVisible;
285 Region DisplayDevice::getDirtyRegion(bool repaintEverything) const {
287 if (repaintEverything) {
288 dirty.set(getBounds());
290 const Transform& planeTransform(mGlobalTransform);
291 dirty = planeTransform.transform(this->dirtyRegion);
292 dirty.andSelf(getBounds());
297 // ----------------------------------------------------------------------------
299 bool DisplayDevice::canDraw() const {
300 return mScreenAcquired;
303 void DisplayDevice::releaseScreen() const {
304 mScreenAcquired = false;
307 void DisplayDevice::acquireScreen() const {
308 mScreenAcquired = true;
311 bool DisplayDevice::isScreenAcquired() const {
312 return mScreenAcquired;
315 // ----------------------------------------------------------------------------
317 void DisplayDevice::setLayerStack(uint32_t stack) {
319 dirtyRegion.set(bounds());
322 // ----------------------------------------------------------------------------
324 status_t DisplayDevice::orientationToTransfrom(
325 int orientation, int w, int h, Transform* tr)
328 switch (orientation) {
329 case DisplayState::eOrientationDefault:
330 flags = Transform::ROT_0;
332 case DisplayState::eOrientation90:
333 flags = Transform::ROT_90;
335 case DisplayState::eOrientation180:
336 flags = Transform::ROT_180;
338 case DisplayState::eOrientation270:
339 flags = Transform::ROT_270;
344 tr->set(flags, w, h);
348 void DisplayDevice::setProjection(int orientation,
349 const Rect& viewport, const Rect& frame) {
350 mOrientation = orientation;
351 mViewport = viewport;
353 updateGeometryTransform();
356 void DisplayDevice::updateGeometryTransform() {
357 int w = mDisplayWidth;
358 int h = mDisplayHeight;
359 Transform TL, TP, R, S;
360 if (DisplayDevice::orientationToTransfrom(
361 mOrientation, w, h, &R) == NO_ERROR) {
362 dirtyRegion.set(bounds());
364 Rect viewport(mViewport);
367 if (!frame.isValid()) {
368 // the destination frame can be invalid if it has never been set,
369 // in that case we assume the whole display frame.
373 if (viewport.isEmpty()) {
374 // viewport can be invalid if it has never been set, in that case
375 // we assume the whole display size.
376 // it's also invalid to have an empty viewport, so we handle that
377 // case in the same way.
378 viewport = Rect(w, h);
379 if (R.getOrientation() & Transform::ROT_90) {
380 // viewport is always specified in the logical orientation
381 // of the display (ie: post-rotation).
382 swap(viewport.right, viewport.bottom);
386 float src_width = viewport.width();
387 float src_height = viewport.height();
388 float dst_width = frame.width();
389 float dst_height = frame.height();
390 if (src_width != dst_width || src_height != dst_height) {
391 float sx = dst_width / src_width;
392 float sy = dst_height / src_height;
396 float src_x = viewport.left;
397 float src_y = viewport.top;
398 float dst_x = frame.left;
399 float dst_y = frame.top;
400 TL.set(-src_x, -src_y);
401 TP.set(dst_x, dst_y);
403 // The viewport and frame are both in the logical orientation.
404 // Apply the logical translation, scale to physical size, apply the
405 // physical translation and finally rotate to the physical orientation.
406 mGlobalTransform = R * TP * S * TL;