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 void DisplayDevice::setDisplayName(const String8& displayName) {
171 if (!displayName.isEmpty()) {
172 // never override the name with an empty name
173 mDisplayName = displayName;
177 uint32_t DisplayDevice::getPageFlipCount() const {
178 return mPageFlipCount;
181 status_t DisplayDevice::compositionComplete() const {
182 if (mFramebufferSurface == NULL) {
185 return mFramebufferSurface->compositionComplete();
188 void DisplayDevice::flip(const Region& dirty) const
192 EGLDisplay dpy = mDisplay;
193 EGLSurface surface = mSurface;
195 #ifdef EGL_ANDROID_swap_rectangle
196 if (mFlags & SWAP_RECTANGLE) {
197 const Region newDirty(dirty.intersect(bounds()));
198 const Rect b(newDirty.getBounds());
199 eglSetSwapRectangleANDROID(dpy, surface,
200 b.left, b.top, b.width(), b.height());
207 void DisplayDevice::swapBuffers(HWComposer& hwc) const {
208 EGLBoolean success = EGL_TRUE;
209 if (hwc.initCheck() != NO_ERROR) {
210 // no HWC, we call eglSwapBuffers()
211 success = eglSwapBuffers(mDisplay, mSurface);
213 // We have a valid HWC, but not all displays can use it, in particular
214 // the virtual displays are on their own.
215 // TODO: HWC 1.2 will allow virtual displays
216 if (mType >= DisplayDevice::DISPLAY_VIRTUAL) {
217 // always call eglSwapBuffers() for virtual displays
218 success = eglSwapBuffers(mDisplay, mSurface);
219 } else if (hwc.supportsFramebufferTarget()) {
220 // as of hwc 1.1 we always call eglSwapBuffers if we have some
222 if (hwc.hasGlesComposition(mType)) {
223 success = eglSwapBuffers(mDisplay, mSurface);
226 // HWC doesn't have the framebuffer target, we don't call
227 // eglSwapBuffers(), since this is handled by HWComposer::commit().
232 EGLint error = eglGetError();
233 if (error == EGL_CONTEXT_LOST ||
234 mType == DisplayDevice::DISPLAY_PRIMARY) {
235 LOG_ALWAYS_FATAL("eglSwapBuffers(%p, %p) failed with 0x%08x",
236 mDisplay, mSurface, error);
241 void DisplayDevice::onSwapBuffersCompleted(HWComposer& hwc) const {
242 if (hwc.initCheck() == NO_ERROR) {
243 if (hwc.supportsFramebufferTarget()) {
244 int fd = hwc.getAndResetReleaseFenceFd(mType);
245 mFramebufferSurface->setReleaseFenceFd(fd);
250 uint32_t DisplayDevice::getFlags() const
255 EGLBoolean DisplayDevice::makeCurrent(EGLDisplay dpy,
256 const sp<const DisplayDevice>& hw, EGLContext ctx) {
257 EGLBoolean result = EGL_TRUE;
258 EGLSurface sur = eglGetCurrentSurface(EGL_DRAW);
259 if (sur != hw->mSurface) {
260 result = eglMakeCurrent(dpy, hw->mSurface, hw->mSurface, ctx);
261 if (result == EGL_TRUE) {
262 setViewportAndProjection(hw);
268 void DisplayDevice::setViewportAndProjection(const sp<const DisplayDevice>& hw) {
269 GLsizei w = hw->mDisplayWidth;
270 GLsizei h = hw->mDisplayHeight;
271 glViewport(0, 0, w, h);
272 glMatrixMode(GL_PROJECTION);
274 // put the origin in the left-bottom corner
275 glOrthof(0, w, 0, h, 0, 1); // l=0, r=w ; b=0, t=h
276 glMatrixMode(GL_MODELVIEW);
279 // ----------------------------------------------------------------------------
281 void DisplayDevice::setVisibleLayersSortedByZ(const Vector< sp<LayerBase> >& layers) {
282 mVisibleLayersSortedByZ = layers;
283 mSecureLayerVisible = false;
284 size_t count = layers.size();
285 for (size_t i=0 ; i<count ; i++) {
286 if (layers[i]->isSecure()) {
287 mSecureLayerVisible = true;
292 const Vector< sp<LayerBase> >& DisplayDevice::getVisibleLayersSortedByZ() const {
293 return mVisibleLayersSortedByZ;
296 bool DisplayDevice::getSecureLayerVisible() const {
297 return mSecureLayerVisible;
300 Region DisplayDevice::getDirtyRegion(bool repaintEverything) const {
302 if (repaintEverything) {
303 dirty.set(getBounds());
305 const Transform& planeTransform(mGlobalTransform);
306 dirty = planeTransform.transform(this->dirtyRegion);
307 dirty.andSelf(getBounds());
312 // ----------------------------------------------------------------------------
314 bool DisplayDevice::canDraw() const {
315 return mScreenAcquired;
318 void DisplayDevice::releaseScreen() const {
319 mScreenAcquired = false;
322 void DisplayDevice::acquireScreen() const {
323 mScreenAcquired = true;
326 bool DisplayDevice::isScreenAcquired() const {
327 return mScreenAcquired;
330 // ----------------------------------------------------------------------------
332 void DisplayDevice::setLayerStack(uint32_t stack) {
334 dirtyRegion.set(bounds());
337 // ----------------------------------------------------------------------------
339 status_t DisplayDevice::orientationToTransfrom(
340 int orientation, int w, int h, Transform* tr)
343 switch (orientation) {
344 case DisplayState::eOrientationDefault:
345 flags = Transform::ROT_0;
347 case DisplayState::eOrientation90:
348 flags = Transform::ROT_90;
350 case DisplayState::eOrientation180:
351 flags = Transform::ROT_180;
353 case DisplayState::eOrientation270:
354 flags = Transform::ROT_270;
359 tr->set(flags, w, h);
363 void DisplayDevice::setProjection(int orientation,
364 const Rect& viewport, const Rect& frame) {
365 mOrientation = orientation;
366 mViewport = viewport;
368 updateGeometryTransform();
371 void DisplayDevice::updateGeometryTransform() {
372 int w = mDisplayWidth;
373 int h = mDisplayHeight;
374 Transform TL, TP, R, S;
375 if (DisplayDevice::orientationToTransfrom(
376 mOrientation, w, h, &R) == NO_ERROR) {
377 dirtyRegion.set(bounds());
379 Rect viewport(mViewport);
382 if (!frame.isValid()) {
383 // the destination frame can be invalid if it has never been set,
384 // in that case we assume the whole display frame.
388 if (viewport.isEmpty()) {
389 // viewport can be invalid if it has never been set, in that case
390 // we assume the whole display size.
391 // it's also invalid to have an empty viewport, so we handle that
392 // case in the same way.
393 viewport = Rect(w, h);
394 if (R.getOrientation() & Transform::ROT_90) {
395 // viewport is always specified in the logical orientation
396 // of the display (ie: post-rotation).
397 swap(viewport.right, viewport.bottom);
401 float src_width = viewport.width();
402 float src_height = viewport.height();
403 float dst_width = frame.width();
404 float dst_height = frame.height();
405 if (src_width != dst_width || src_height != dst_height) {
406 float sx = dst_width / src_width;
407 float sy = dst_height / src_height;
411 float src_x = viewport.left;
412 float src_y = viewport.top;
413 float dst_x = frame.left;
414 float dst_y = frame.top;
415 TL.set(-src_x, -src_y);
416 TP.set(dst_x, dst_y);
418 // The viewport and frame are both in the logical orientation.
419 // Apply the logical translation, scale to physical size, apply the
420 // physical translation and finally rotate to the physical orientation.
421 mGlobalTransform = R * TP * S * TL;
423 const uint8_t type = mGlobalTransform.getType();
424 mNeedsFiltering = (!mGlobalTransform.preserveRects() ||
425 (type >= Transform::SCALE));
429 void DisplayDevice::dump(String8& result, char* buffer, size_t SIZE) const {
430 const Transform& tr(mGlobalTransform);
431 snprintf(buffer, SIZE,
432 "+ DisplayDevice: %s\n"
433 " type=%x, layerStack=%u, (%4dx%4d), ANativeWindow=%p, orient=%2d (type=%08x), "
434 "flips=%u, secure=%d, acquired=%d, numLayers=%u\n"
435 " v:[%d,%d,%d,%d], f:[%d,%d,%d,%d], "
436 "transform:[[%0.3f,%0.3f,%0.3f][%0.3f,%0.3f,%0.3f][%0.3f,%0.3f,%0.3f]]\n",
437 mDisplayName.string(), mType,
438 mLayerStack, mDisplayWidth, mDisplayHeight, mNativeWindow.get(),
439 mOrientation, tr.getType(), getPageFlipCount(),
440 mSecureLayerVisible, mScreenAcquired, mVisibleLayersSortedByZ.size(),
441 mViewport.left, mViewport.top, mViewport.right, mViewport.bottom,
442 mFrame.left, mFrame.top, mFrame.right, mFrame.bottom,
443 tr[0][0], tr[1][0], tr[2][0],
444 tr[0][1], tr[1][1], tr[2][1],
445 tr[0][2], tr[1][2], tr[2][2]);
447 result.append(buffer);
449 String8 fbtargetDump;
450 if (mFramebufferSurface != NULL) {
451 mFramebufferSurface->dump(fbtargetDump);
452 result.append(fbtargetDump);