GoogleGit

blob: 69b9c343cce611dda2cb4866f0e8607fbe0a5ba1 [file] [log] [blame]
  1. /*
  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. #include <stdlib.h>
  17. #include <stdio.h>
  18. #include <string.h>
  19. #include <math.h>
  20. #include <cutils/properties.h>
  21. #include <utils/RefBase.h>
  22. #include <utils/Log.h>
  23. #include <ui/DisplayInfo.h>
  24. #include <ui/PixelFormat.h>
  25. #include <gui/SurfaceTextureClient.h>
  26. #include <GLES/gl.h>
  27. #include <EGL/egl.h>
  28. #include <EGL/eglext.h>
  29. #include <hardware/gralloc.h>
  30. #include "DisplayHardware/FramebufferSurface.h"
  31. #include "DisplayHardware/HWComposer.h"
  32. #include "clz.h"
  33. #include "DisplayDevice.h"
  34. #include "GLExtensions.h"
  35. #include "SurfaceFlinger.h"
  36. #include "LayerBase.h"
  37. // ----------------------------------------------------------------------------
  38. using namespace android;
  39. // ----------------------------------------------------------------------------
  40. static __attribute__((noinline))
  41. void checkGLErrors()
  42. {
  43. do {
  44. // there could be more than one error flag
  45. GLenum error = glGetError();
  46. if (error == GL_NO_ERROR)
  47. break;
  48. ALOGE("GL error 0x%04x", int(error));
  49. } while(true);
  50. }
  51. // ----------------------------------------------------------------------------
  52. /*
  53. * Initialize the display to the specified values.
  54. *
  55. */
  56. DisplayDevice::DisplayDevice(
  57. const sp<SurfaceFlinger>& flinger,
  58. DisplayType type,
  59. bool isSecure,
  60. const wp<IBinder>& displayToken,
  61. const sp<ANativeWindow>& nativeWindow,
  62. const sp<FramebufferSurface>& framebufferSurface,
  63. EGLConfig config)
  64. : mFlinger(flinger),
  65. mType(type), mHwcDisplayId(-1),
  66. mDisplayToken(displayToken),
  67. mNativeWindow(nativeWindow),
  68. mFramebufferSurface(framebufferSurface),
  69. mDisplay(EGL_NO_DISPLAY),
  70. mSurface(EGL_NO_SURFACE),
  71. mContext(EGL_NO_CONTEXT),
  72. mDisplayWidth(), mDisplayHeight(), mFormat(),
  73. mFlags(),
  74. mPageFlipCount(),
  75. mIsSecure(isSecure),
  76. mSecureLayerVisible(false),
  77. mScreenAcquired(false),
  78. mLayerStack(0),
  79. mOrientation()
  80. {
  81. init(config);
  82. }
  83. DisplayDevice::~DisplayDevice() {
  84. if (mSurface != EGL_NO_SURFACE) {
  85. eglDestroySurface(mDisplay, mSurface);
  86. mSurface = EGL_NO_SURFACE;
  87. }
  88. }
  89. bool DisplayDevice::isValid() const {
  90. return mFlinger != NULL;
  91. }
  92. int DisplayDevice::getWidth() const {
  93. return mDisplayWidth;
  94. }
  95. int DisplayDevice::getHeight() const {
  96. return mDisplayHeight;
  97. }
  98. PixelFormat DisplayDevice::getFormat() const {
  99. return mFormat;
  100. }
  101. EGLSurface DisplayDevice::getEGLSurface() const {
  102. return mSurface;
  103. }
  104. void DisplayDevice::init(EGLConfig config)
  105. {
  106. ANativeWindow* const window = mNativeWindow.get();
  107. int format;
  108. window->query(window, NATIVE_WINDOW_FORMAT, &format);
  109. /*
  110. * Create our display's surface
  111. */
  112. EGLSurface surface;
  113. EGLint w, h;
  114. EGLDisplay display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
  115. surface = eglCreateWindowSurface(display, config, window, NULL);
  116. eglQuerySurface(display, surface, EGL_WIDTH, &mDisplayWidth);
  117. eglQuerySurface(display, surface, EGL_HEIGHT, &mDisplayHeight);
  118. mDisplay = display;
  119. mSurface = surface;
  120. mFormat = format;
  121. mPageFlipCount = 0;
  122. mViewport.makeInvalid();
  123. mFrame.makeInvalid();
  124. // external displays are always considered enabled
  125. mScreenAcquired = (mType >= DisplayDevice::NUM_DISPLAY_TYPES);
  126. // get an h/w composer ID
  127. mHwcDisplayId = mFlinger->allocateHwcDisplayId(mType);
  128. // Name the display. The name will be replaced shortly if the display
  129. // was created with createDisplay().
  130. switch (mType) {
  131. case DISPLAY_PRIMARY:
  132. mDisplayName = "Built-in Screen";
  133. break;
  134. case DISPLAY_EXTERNAL:
  135. mDisplayName = "HDMI Screen";
  136. break;
  137. default:
  138. mDisplayName = "Virtual Screen"; // e.g. Overlay #n
  139. break;
  140. }
  141. // initialize the display orientation transform.
  142. setProjection(DisplayState::eOrientationDefault, mViewport, mFrame);
  143. }
  144. void DisplayDevice::setDisplayName(const String8& displayName) {
  145. if (!displayName.isEmpty()) {
  146. // never override the name with an empty name
  147. mDisplayName = displayName;
  148. }
  149. }
  150. uint32_t DisplayDevice::getPageFlipCount() const {
  151. return mPageFlipCount;
  152. }
  153. status_t DisplayDevice::compositionComplete() const {
  154. if (mFramebufferSurface == NULL) {
  155. return NO_ERROR;
  156. }
  157. return mFramebufferSurface->compositionComplete();
  158. }
  159. void DisplayDevice::flip(const Region& dirty) const
  160. {
  161. checkGLErrors();
  162. EGLDisplay dpy = mDisplay;
  163. EGLSurface surface = mSurface;
  164. #ifdef EGL_ANDROID_swap_rectangle
  165. if (mFlags & SWAP_RECTANGLE) {
  166. const Region newDirty(dirty.intersect(bounds()));
  167. const Rect b(newDirty.getBounds());
  168. eglSetSwapRectangleANDROID(dpy, surface,
  169. b.left, b.top, b.width(), b.height());
  170. }
  171. #endif
  172. mPageFlipCount++;
  173. }
  174. void DisplayDevice::swapBuffers(HWComposer& hwc) const {
  175. EGLBoolean success = EGL_TRUE;
  176. if (hwc.initCheck() != NO_ERROR) {
  177. // no HWC, we call eglSwapBuffers()
  178. success = eglSwapBuffers(mDisplay, mSurface);
  179. } else {
  180. // We have a valid HWC, but not all displays can use it, in particular
  181. // the virtual displays are on their own.
  182. // TODO: HWC 1.2 will allow virtual displays
  183. if (mType >= DisplayDevice::DISPLAY_VIRTUAL) {
  184. // always call eglSwapBuffers() for virtual displays
  185. success = eglSwapBuffers(mDisplay, mSurface);
  186. } else if (hwc.supportsFramebufferTarget()) {
  187. // as of hwc 1.1 we always call eglSwapBuffers if we have some
  188. // GLES layers
  189. if (hwc.hasGlesComposition(mType)) {
  190. success = eglSwapBuffers(mDisplay, mSurface);
  191. }
  192. } else {
  193. // HWC doesn't have the framebuffer target, we don't call
  194. // eglSwapBuffers(), since this is handled by HWComposer::commit().
  195. }
  196. }
  197. if (!success) {
  198. EGLint error = eglGetError();
  199. if (error == EGL_CONTEXT_LOST ||
  200. mType == DisplayDevice::DISPLAY_PRIMARY) {
  201. LOG_ALWAYS_FATAL("eglSwapBuffers(%p, %p) failed with 0x%08x",
  202. mDisplay, mSurface, error);
  203. }
  204. }
  205. }
  206. void DisplayDevice::onSwapBuffersCompleted(HWComposer& hwc) const {
  207. if (hwc.initCheck() == NO_ERROR) {
  208. if (hwc.supportsFramebufferTarget()) {
  209. int fd = hwc.getAndResetReleaseFenceFd(mType);
  210. mFramebufferSurface->setReleaseFenceFd(fd);
  211. }
  212. }
  213. }
  214. uint32_t DisplayDevice::getFlags() const
  215. {
  216. return mFlags;
  217. }
  218. EGLBoolean DisplayDevice::makeCurrent(EGLDisplay dpy,
  219. const sp<const DisplayDevice>& hw, EGLContext ctx) {
  220. EGLBoolean result = EGL_TRUE;
  221. EGLSurface sur = eglGetCurrentSurface(EGL_DRAW);
  222. if (sur != hw->mSurface) {
  223. result = eglMakeCurrent(dpy, hw->mSurface, hw->mSurface, ctx);
  224. if (result == EGL_TRUE) {
  225. setViewportAndProjection(hw);
  226. }
  227. }
  228. return result;
  229. }
  230. void DisplayDevice::setViewportAndProjection(const sp<const DisplayDevice>& hw) {
  231. GLsizei w = hw->mDisplayWidth;
  232. GLsizei h = hw->mDisplayHeight;
  233. glViewport(0, 0, w, h);
  234. glMatrixMode(GL_PROJECTION);
  235. glLoadIdentity();
  236. // put the origin in the left-bottom corner
  237. glOrthof(0, w, 0, h, 0, 1); // l=0, r=w ; b=0, t=h
  238. glMatrixMode(GL_MODELVIEW);
  239. }
  240. // ----------------------------------------------------------------------------
  241. void DisplayDevice::setVisibleLayersSortedByZ(const Vector< sp<LayerBase> >& layers) {
  242. mVisibleLayersSortedByZ = layers;
  243. mSecureLayerVisible = false;
  244. size_t count = layers.size();
  245. for (size_t i=0 ; i<count ; i++) {
  246. if (layers[i]->isSecure()) {
  247. mSecureLayerVisible = true;
  248. }
  249. }
  250. }
  251. const Vector< sp<LayerBase> >& DisplayDevice::getVisibleLayersSortedByZ() const {
  252. return mVisibleLayersSortedByZ;
  253. }
  254. bool DisplayDevice::getSecureLayerVisible() const {
  255. return mSecureLayerVisible;
  256. }
  257. Region DisplayDevice::getDirtyRegion(bool repaintEverything) const {
  258. Region dirty;
  259. if (repaintEverything) {
  260. dirty.set(getBounds());
  261. } else {
  262. const Transform& planeTransform(mGlobalTransform);
  263. dirty = planeTransform.transform(this->dirtyRegion);
  264. dirty.andSelf(getBounds());
  265. }
  266. return dirty;
  267. }
  268. // ----------------------------------------------------------------------------
  269. bool DisplayDevice::canDraw() const {
  270. return mScreenAcquired;
  271. }
  272. void DisplayDevice::releaseScreen() const {
  273. mScreenAcquired = false;
  274. }
  275. void DisplayDevice::acquireScreen() const {
  276. mScreenAcquired = true;
  277. }
  278. bool DisplayDevice::isScreenAcquired() const {
  279. return mScreenAcquired;
  280. }
  281. // ----------------------------------------------------------------------------
  282. void DisplayDevice::setLayerStack(uint32_t stack) {
  283. mLayerStack = stack;
  284. dirtyRegion.set(bounds());
  285. }
  286. // ----------------------------------------------------------------------------
  287. status_t DisplayDevice::orientationToTransfrom(
  288. int orientation, int w, int h, Transform* tr)
  289. {
  290. uint32_t flags = 0;
  291. switch (orientation) {
  292. case DisplayState::eOrientationDefault:
  293. flags = Transform::ROT_0;
  294. break;
  295. case DisplayState::eOrientation90:
  296. flags = Transform::ROT_90;
  297. break;
  298. case DisplayState::eOrientation180:
  299. flags = Transform::ROT_180;
  300. break;
  301. case DisplayState::eOrientation270:
  302. flags = Transform::ROT_270;
  303. break;
  304. default:
  305. return BAD_VALUE;
  306. }
  307. tr->set(flags, w, h);
  308. return NO_ERROR;
  309. }
  310. void DisplayDevice::setProjection(int orientation,
  311. const Rect& viewport, const Rect& frame) {
  312. mOrientation = orientation;
  313. mViewport = viewport;
  314. mFrame = frame;
  315. updateGeometryTransform();
  316. }
  317. void DisplayDevice::updateGeometryTransform() {
  318. int w = mDisplayWidth;
  319. int h = mDisplayHeight;
  320. Transform TL, TP, R, S;
  321. if (DisplayDevice::orientationToTransfrom(
  322. mOrientation, w, h, &R) == NO_ERROR) {
  323. dirtyRegion.set(bounds());
  324. Rect viewport(mViewport);
  325. Rect frame(mFrame);
  326. if (!frame.isValid()) {
  327. // the destination frame can be invalid if it has never been set,
  328. // in that case we assume the whole display frame.
  329. frame = Rect(w, h);
  330. }
  331. if (viewport.isEmpty()) {
  332. // viewport can be invalid if it has never been set, in that case
  333. // we assume the whole display size.
  334. // it's also invalid to have an empty viewport, so we handle that
  335. // case in the same way.
  336. viewport = Rect(w, h);
  337. if (R.getOrientation() & Transform::ROT_90) {
  338. // viewport is always specified in the logical orientation
  339. // of the display (ie: post-rotation).
  340. swap(viewport.right, viewport.bottom);
  341. }
  342. }
  343. float src_width = viewport.width();
  344. float src_height = viewport.height();
  345. float dst_width = frame.width();
  346. float dst_height = frame.height();
  347. if (src_width != dst_width || src_height != dst_height) {
  348. float sx = dst_width / src_width;
  349. float sy = dst_height / src_height;
  350. S.set(sx, 0, 0, sy);
  351. }
  352. float src_x = viewport.left;
  353. float src_y = viewport.top;
  354. float dst_x = frame.left;
  355. float dst_y = frame.top;
  356. TL.set(-src_x, -src_y);
  357. TP.set(dst_x, dst_y);
  358. // The viewport and frame are both in the logical orientation.
  359. // Apply the logical translation, scale to physical size, apply the
  360. // physical translation and finally rotate to the physical orientation.
  361. mGlobalTransform = R * TP * S * TL;
  362. const uint8_t type = mGlobalTransform.getType();
  363. mNeedsFiltering = (!mGlobalTransform.preserveRects() ||
  364. (type >= Transform::SCALE));
  365. }
  366. }
  367. void DisplayDevice::dump(String8& result, char* buffer, size_t SIZE) const {
  368. const Transform& tr(mGlobalTransform);
  369. snprintf(buffer, SIZE,
  370. "+ DisplayDevice: %s\n"
  371. " type=%x, layerStack=%u, (%4dx%4d), ANativeWindow=%p, orient=%2d (type=%08x), "
  372. "flips=%u, isSecure=%d, secureVis=%d, acquired=%d, numLayers=%u\n"
  373. " v:[%d,%d,%d,%d], f:[%d,%d,%d,%d], "
  374. "transform:[[%0.3f,%0.3f,%0.3f][%0.3f,%0.3f,%0.3f][%0.3f,%0.3f,%0.3f]]\n",
  375. mDisplayName.string(), mType,
  376. mLayerStack, mDisplayWidth, mDisplayHeight, mNativeWindow.get(),
  377. mOrientation, tr.getType(), getPageFlipCount(),
  378. mIsSecure, mSecureLayerVisible, mScreenAcquired, mVisibleLayersSortedByZ.size(),
  379. mViewport.left, mViewport.top, mViewport.right, mViewport.bottom,
  380. mFrame.left, mFrame.top, mFrame.right, mFrame.bottom,
  381. tr[0][0], tr[1][0], tr[2][0],
  382. tr[0][1], tr[1][1], tr[2][1],
  383. tr[0][2], tr[1][2], tr[2][2]);
  384. result.append(buffer);
  385. String8 fbtargetDump;
  386. if (mFramebufferSurface != NULL) {
  387. mFramebufferSurface->dump(fbtargetDump);
  388. result.append(fbtargetDump);
  389. }
  390. }