DisplayDevice.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561
  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/Surface.h>
  26. #include <hardware/gralloc.h>
  27. #include "DisplayHardware/DisplaySurface.h"
  28. #include "DisplayHardware/HWComposer.h"
  29. #include "RenderEngine/RenderEngine.h"
  30. #include "clz.h"
  31. #include "DisplayDevice.h"
  32. #include "SurfaceFlinger.h"
  33. #include "Layer.h"
  34. // ----------------------------------------------------------------------------
  35. using namespace android;
  36. // ----------------------------------------------------------------------------
  37. #ifdef EGL_ANDROID_swap_rectangle
  38. static constexpr bool kEGLAndroidSwapRectangle = true;
  39. #else
  40. static constexpr bool kEGLAndroidSwapRectangle = false;
  41. #endif
  42. #if !defined(EGL_EGLEXT_PROTOTYPES) || !defined(EGL_ANDROID_swap_rectangle)
  43. // Dummy implementation in case it is missing.
  44. inline void eglSetSwapRectangleANDROID (EGLDisplay, EGLSurface, EGLint, EGLint, EGLint, EGLint) {
  45. }
  46. #endif
  47. /*
  48. * Initialize the display to the specified values.
  49. *
  50. */
  51. DisplayDevice::DisplayDevice(
  52. const sp<SurfaceFlinger>& flinger,
  53. DisplayType type,
  54. int32_t hwcId,
  55. int format,
  56. bool isSecure,
  57. const wp<IBinder>& displayToken,
  58. const sp<DisplaySurface>& displaySurface,
  59. const sp<IGraphicBufferProducer>& producer,
  60. EGLConfig config)
  61. : lastCompositionHadVisibleLayers(false),
  62. mFlinger(flinger),
  63. mType(type), mHwcDisplayId(hwcId),
  64. mDisplayToken(displayToken),
  65. mDisplaySurface(displaySurface),
  66. mDisplay(EGL_NO_DISPLAY),
  67. mSurface(EGL_NO_SURFACE),
  68. mDisplayWidth(), mDisplayHeight(), mFormat(),
  69. mFlags(),
  70. mPageFlipCount(),
  71. mIsSecure(isSecure),
  72. mSecureLayerVisible(false),
  73. mLayerStack(NO_LAYER_STACK),
  74. mOrientation(),
  75. mPowerMode(HWC_POWER_MODE_OFF),
  76. mActiveConfig(0)
  77. {
  78. Surface* surface;
  79. mNativeWindow = surface = new Surface(producer, false);
  80. ANativeWindow* const window = mNativeWindow.get();
  81. char property[PROPERTY_VALUE_MAX];
  82. /*
  83. * Create our display's surface
  84. */
  85. EGLSurface eglSurface;
  86. EGLDisplay display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
  87. if (config == EGL_NO_CONFIG) {
  88. config = RenderEngine::chooseEglConfig(display, format);
  89. }
  90. eglSurface = eglCreateWindowSurface(display, config, window, NULL);
  91. eglQuerySurface(display, eglSurface, EGL_WIDTH, &mDisplayWidth);
  92. eglQuerySurface(display, eglSurface, EGL_HEIGHT, &mDisplayHeight);
  93. // Make sure that composition can never be stalled by a virtual display
  94. // consumer that isn't processing buffers fast enough. We have to do this
  95. // in two places:
  96. // * Here, in case the display is composed entirely by HWC.
  97. // * In makeCurrent(), using eglSwapInterval. Some EGL drivers set the
  98. // window's swap interval in eglMakeCurrent, so they'll override the
  99. // interval we set here.
  100. if (mType >= DisplayDevice::DISPLAY_VIRTUAL)
  101. window->setSwapInterval(window, 0);
  102. mConfig = config;
  103. mDisplay = display;
  104. mSurface = eglSurface;
  105. mFormat = format;
  106. mPageFlipCount = 0;
  107. mViewport.makeInvalid();
  108. mFrame.makeInvalid();
  109. // virtual displays are always considered enabled
  110. mPowerMode = (mType >= DisplayDevice::DISPLAY_VIRTUAL) ?
  111. HWC_POWER_MODE_NORMAL : HWC_POWER_MODE_OFF;
  112. // Name the display. The name will be replaced shortly if the display
  113. // was created with createDisplay().
  114. switch (mType) {
  115. case DISPLAY_PRIMARY:
  116. mDisplayName = "Built-in Screen";
  117. break;
  118. case DISPLAY_EXTERNAL:
  119. mDisplayName = "HDMI Screen";
  120. break;
  121. default:
  122. mDisplayName = "Virtual Screen"; // e.g. Overlay #n
  123. break;
  124. }
  125. mPanelMountFlip = 0;
  126. // 1: H-Flip, 2: V-Flip, 3: 180 (HV Flip)
  127. property_get("persist.panel.mountflip", property, "0");
  128. mPanelMountFlip = atoi(property);
  129. // initialize the display orientation transform.
  130. setProjection(DisplayState::eOrientationDefault, mViewport, mFrame);
  131. #ifdef NUM_FRAMEBUFFER_SURFACE_BUFFERS
  132. surface->allocateBuffers();
  133. #endif
  134. }
  135. DisplayDevice::~DisplayDevice() {
  136. if (mSurface != EGL_NO_SURFACE) {
  137. eglDestroySurface(mDisplay, mSurface);
  138. mSurface = EGL_NO_SURFACE;
  139. }
  140. }
  141. void DisplayDevice::disconnect(HWComposer& hwc) {
  142. if (mHwcDisplayId >= 0) {
  143. hwc.disconnectDisplay(mHwcDisplayId);
  144. if (mHwcDisplayId >= DISPLAY_VIRTUAL)
  145. hwc.freeDisplayId(mHwcDisplayId);
  146. mHwcDisplayId = -1;
  147. }
  148. }
  149. bool DisplayDevice::isValid() const {
  150. return mFlinger != NULL;
  151. }
  152. int DisplayDevice::getWidth() const {
  153. return mDisplayWidth;
  154. }
  155. int DisplayDevice::getHeight() const {
  156. return mDisplayHeight;
  157. }
  158. PixelFormat DisplayDevice::getFormat() const {
  159. return mFormat;
  160. }
  161. EGLSurface DisplayDevice::getEGLSurface() const {
  162. return mSurface;
  163. }
  164. void DisplayDevice::setDisplayName(const String8& displayName) {
  165. if (!displayName.isEmpty()) {
  166. // never override the name with an empty name
  167. mDisplayName = displayName;
  168. }
  169. }
  170. uint32_t DisplayDevice::getPageFlipCount() const {
  171. return mPageFlipCount;
  172. }
  173. status_t DisplayDevice::compositionComplete() const {
  174. return mDisplaySurface->compositionComplete();
  175. }
  176. void DisplayDevice::flip(const Region& dirty) const
  177. {
  178. mFlinger->getRenderEngine().checkErrors();
  179. if (kEGLAndroidSwapRectangle) {
  180. if (mFlags & SWAP_RECTANGLE) {
  181. const Region newDirty(dirty.intersect(bounds()));
  182. const Rect b(newDirty.getBounds());
  183. eglSetSwapRectangleANDROID(mDisplay, mSurface,
  184. b.left, b.top, b.width(), b.height());
  185. }
  186. }
  187. mPageFlipCount++;
  188. }
  189. status_t DisplayDevice::beginFrame(bool mustRecompose) const {
  190. return mDisplaySurface->beginFrame(mustRecompose);
  191. }
  192. status_t DisplayDevice::prepareFrame(const HWComposer& hwc) const {
  193. DisplaySurface::CompositionType compositionType;
  194. bool haveGles = hwc.hasGlesComposition(mHwcDisplayId);
  195. bool haveHwc = hwc.hasHwcComposition(mHwcDisplayId);
  196. if (haveGles && haveHwc) {
  197. compositionType = DisplaySurface::COMPOSITION_MIXED;
  198. } else if (haveGles) {
  199. compositionType = DisplaySurface::COMPOSITION_GLES;
  200. } else if (haveHwc) {
  201. compositionType = DisplaySurface::COMPOSITION_HWC;
  202. } else {
  203. // Nothing to do -- when turning the screen off we get a frame like
  204. // this. Call it a HWC frame since we won't be doing any GLES work but
  205. // will do a prepare/set cycle.
  206. compositionType = DisplaySurface::COMPOSITION_HWC;
  207. }
  208. return mDisplaySurface->prepareFrame(compositionType);
  209. }
  210. void DisplayDevice::swapBuffers(HWComposer& hwc) const {
  211. // We need to call eglSwapBuffers() if:
  212. // (1) we don't have a hardware composer, or
  213. // (2) we did GLES composition this frame, and either
  214. // (a) we have framebuffer target support (not present on legacy
  215. // devices, where HWComposer::commit() handles things); or
  216. // (b) this is a virtual display
  217. if (hwc.initCheck() != NO_ERROR ||
  218. (hwc.hasGlesComposition(mHwcDisplayId) &&
  219. (hwc.supportsFramebufferTarget() || mType >= DISPLAY_VIRTUAL))) {
  220. EGLBoolean success = eglSwapBuffers(mDisplay, mSurface);
  221. if (!success) {
  222. EGLint error = eglGetError();
  223. if (error == EGL_CONTEXT_LOST ||
  224. mType == DisplayDevice::DISPLAY_PRIMARY) {
  225. LOG_ALWAYS_FATAL("eglSwapBuffers(%p, %p) failed with 0x%08x",
  226. mDisplay, mSurface, error);
  227. } else {
  228. ALOGE("eglSwapBuffers(%p, %p) failed with 0x%08x",
  229. mDisplay, mSurface, error);
  230. }
  231. }
  232. }
  233. status_t result = mDisplaySurface->advanceFrame();
  234. if (result != NO_ERROR) {
  235. ALOGE("[%s] failed pushing new frame to HWC: %d",
  236. mDisplayName.string(), result);
  237. }
  238. }
  239. void DisplayDevice::onSwapBuffersCompleted(HWComposer& hwc) const {
  240. if (hwc.initCheck() == NO_ERROR) {
  241. mDisplaySurface->onFrameCommitted();
  242. }
  243. }
  244. uint32_t DisplayDevice::getFlags() const
  245. {
  246. return mFlags;
  247. }
  248. EGLBoolean DisplayDevice::makeCurrent(EGLDisplay dpy, EGLContext ctx) const {
  249. EGLBoolean result = EGL_TRUE;
  250. EGLSurface sur = eglGetCurrentSurface(EGL_DRAW);
  251. if (sur != mSurface) {
  252. result = eglMakeCurrent(dpy, mSurface, mSurface, ctx);
  253. if (result == EGL_TRUE) {
  254. if (mType >= DisplayDevice::DISPLAY_VIRTUAL)
  255. eglSwapInterval(dpy, 0);
  256. }
  257. }
  258. setViewportAndProjection();
  259. return result;
  260. }
  261. void DisplayDevice::setViewportAndProjection() const {
  262. size_t w = mDisplayWidth;
  263. size_t h = mDisplayHeight;
  264. Rect sourceCrop(0, 0, w, h);
  265. mFlinger->getRenderEngine().setViewportAndProjection(w, h, sourceCrop, h,
  266. false, Transform::ROT_0);
  267. }
  268. // ----------------------------------------------------------------------------
  269. void DisplayDevice::setVisibleLayersSortedByZ(const Vector< sp<Layer> >& layers) {
  270. mVisibleLayersSortedByZ = layers;
  271. mSecureLayerVisible = false;
  272. size_t count = layers.size();
  273. for (size_t i=0 ; i<count ; i++) {
  274. const sp<Layer>& layer(layers[i]);
  275. if (layer->isSecure()) {
  276. mSecureLayerVisible = true;
  277. }
  278. }
  279. }
  280. const Vector< sp<Layer> >& DisplayDevice::getVisibleLayersSortedByZ() const {
  281. return mVisibleLayersSortedByZ;
  282. }
  283. bool DisplayDevice::getSecureLayerVisible() const {
  284. return mSecureLayerVisible;
  285. }
  286. Region DisplayDevice::getDirtyRegion(bool repaintEverything) const {
  287. Region dirty;
  288. if (repaintEverything) {
  289. dirty.set(getBounds());
  290. } else {
  291. const Transform& planeTransform(mGlobalTransform);
  292. dirty = planeTransform.transform(this->dirtyRegion);
  293. dirty.andSelf(getBounds());
  294. }
  295. return dirty;
  296. }
  297. // ----------------------------------------------------------------------------
  298. void DisplayDevice::setPowerMode(int mode) {
  299. mPowerMode = mode;
  300. }
  301. int DisplayDevice::getPowerMode() const {
  302. return mPowerMode;
  303. }
  304. bool DisplayDevice::isDisplayOn() const {
  305. return (mPowerMode != HWC_POWER_MODE_OFF);
  306. }
  307. // ----------------------------------------------------------------------------
  308. void DisplayDevice::setActiveConfig(int mode) {
  309. mActiveConfig = mode;
  310. }
  311. int DisplayDevice::getActiveConfig() const {
  312. return mActiveConfig;
  313. }
  314. // ----------------------------------------------------------------------------
  315. void DisplayDevice::setLayerStack(uint32_t stack) {
  316. mLayerStack = stack;
  317. dirtyRegion.set(bounds());
  318. }
  319. // ----------------------------------------------------------------------------
  320. uint32_t DisplayDevice::getOrientationTransform() const {
  321. uint32_t transform = 0;
  322. switch (mOrientation) {
  323. case DisplayState::eOrientationDefault:
  324. transform = Transform::ROT_0;
  325. break;
  326. case DisplayState::eOrientation90:
  327. transform = Transform::ROT_90;
  328. break;
  329. case DisplayState::eOrientation180:
  330. transform = Transform::ROT_180;
  331. break;
  332. case DisplayState::eOrientation270:
  333. transform = Transform::ROT_270;
  334. break;
  335. }
  336. return transform;
  337. }
  338. status_t DisplayDevice::orientationToTransfrom(
  339. int orientation, int w, int h, Transform* tr)
  340. {
  341. uint32_t flags = 0;
  342. char value[PROPERTY_VALUE_MAX];
  343. property_get("ro.sf.hwrotation", value, "0");
  344. int additionalRot = atoi(value);
  345. if (additionalRot && mType == DISPLAY_PRIMARY) {
  346. additionalRot /= 90;
  347. if (orientation == DisplayState::eOrientationUnchanged) {
  348. orientation = additionalRot;
  349. } else {
  350. orientation += additionalRot;
  351. orientation %= 4;
  352. }
  353. }
  354. switch (orientation) {
  355. case DisplayState::eOrientationDefault:
  356. flags = Transform::ROT_0;
  357. break;
  358. case DisplayState::eOrientation90:
  359. flags = Transform::ROT_90;
  360. break;
  361. case DisplayState::eOrientation180:
  362. flags = Transform::ROT_180;
  363. break;
  364. case DisplayState::eOrientation270:
  365. flags = Transform::ROT_270;
  366. break;
  367. default:
  368. return BAD_VALUE;
  369. }
  370. if (DISPLAY_PRIMARY == mHwcDisplayId) {
  371. flags = flags ^ getPanelMountFlip();
  372. }
  373. tr->set(flags, w, h);
  374. return NO_ERROR;
  375. }
  376. void DisplayDevice::setDisplaySize(const int newWidth, const int newHeight) {
  377. dirtyRegion.set(getBounds());
  378. if (mSurface != EGL_NO_SURFACE) {
  379. eglDestroySurface(mDisplay, mSurface);
  380. mSurface = EGL_NO_SURFACE;
  381. }
  382. mDisplaySurface->resizeBuffers(newWidth, newHeight);
  383. ANativeWindow* const window = mNativeWindow.get();
  384. mSurface = eglCreateWindowSurface(mDisplay, mConfig, window, NULL);
  385. eglQuerySurface(mDisplay, mSurface, EGL_WIDTH, &mDisplayWidth);
  386. eglQuerySurface(mDisplay, mSurface, EGL_HEIGHT, &mDisplayHeight);
  387. LOG_FATAL_IF(mDisplayWidth != newWidth,
  388. "Unable to set new width to %d", newWidth);
  389. LOG_FATAL_IF(mDisplayHeight != newHeight,
  390. "Unable to set new height to %d", newHeight);
  391. }
  392. void DisplayDevice::setProjection(int orientation,
  393. const Rect& newViewport, const Rect& newFrame) {
  394. Rect viewport(newViewport);
  395. Rect frame(newFrame);
  396. const int w = mDisplayWidth;
  397. const int h = mDisplayHeight;
  398. Transform R;
  399. DisplayDevice::orientationToTransfrom(orientation, w, h, &R);
  400. if (!frame.isValid()) {
  401. // the destination frame can be invalid if it has never been set,
  402. // in that case we assume the whole display frame.
  403. char value[PROPERTY_VALUE_MAX];
  404. property_get("ro.sf.hwrotation", value, "0");
  405. int additionalRot = atoi(value);
  406. if (additionalRot == 90 || additionalRot == 270) {
  407. frame = Rect(h, w);
  408. } else {
  409. frame = Rect(w, h);
  410. }
  411. }
  412. if (viewport.isEmpty()) {
  413. // viewport can be invalid if it has never been set, in that case
  414. // we assume the whole display size.
  415. // it's also invalid to have an empty viewport, so we handle that
  416. // case in the same way.
  417. viewport = Rect(w, h);
  418. if (R.getOrientation() & Transform::ROT_90) {
  419. // viewport is always specified in the logical orientation
  420. // of the display (ie: post-rotation).
  421. swap(viewport.right, viewport.bottom);
  422. }
  423. }
  424. dirtyRegion.set(getBounds());
  425. Transform TL, TP, S;
  426. float src_width = viewport.width();
  427. float src_height = viewport.height();
  428. float dst_width = frame.width();
  429. float dst_height = frame.height();
  430. if (src_width != dst_width || src_height != dst_height) {
  431. float sx = dst_width / src_width;
  432. float sy = dst_height / src_height;
  433. S.set(sx, 0, 0, sy);
  434. }
  435. float src_x = viewport.left;
  436. float src_y = viewport.top;
  437. float dst_x = frame.left;
  438. float dst_y = frame.top;
  439. TL.set(-src_x, -src_y);
  440. TP.set(dst_x, dst_y);
  441. // The viewport and frame are both in the logical orientation.
  442. // Apply the logical translation, scale to physical size, apply the
  443. // physical translation and finally rotate to the physical orientation.
  444. mGlobalTransform = R * TP * S * TL;
  445. const uint8_t type = mGlobalTransform.getType();
  446. mNeedsFiltering = (!mGlobalTransform.preserveRects() ||
  447. (type >= Transform::SCALE));
  448. mScissor = mGlobalTransform.transform(viewport);
  449. if (mScissor.isEmpty()) {
  450. mScissor = getBounds();
  451. }
  452. mOrientation = orientation;
  453. mViewport = viewport;
  454. mFrame = frame;
  455. }
  456. void DisplayDevice::dump(String8& result) const {
  457. const Transform& tr(mGlobalTransform);
  458. result.appendFormat(
  459. "+ DisplayDevice: %s\n"
  460. " type=%x, hwcId=%d, layerStack=%u, (%4dx%4d), ANativeWindow=%p, orient=%2d (type=%08x), "
  461. "flips=%u, isSecure=%d, secureVis=%d, powerMode=%d, activeConfig=%d, numLayers=%zu\n"
  462. " v:[%d,%d,%d,%d], f:[%d,%d,%d,%d], s:[%d,%d,%d,%d],"
  463. "transform:[[%0.3f,%0.3f,%0.3f][%0.3f,%0.3f,%0.3f][%0.3f,%0.3f,%0.3f]]\n",
  464. mDisplayName.string(), mType, mHwcDisplayId,
  465. mLayerStack, mDisplayWidth, mDisplayHeight, mNativeWindow.get(),
  466. mOrientation, tr.getType(), getPageFlipCount(),
  467. mIsSecure, mSecureLayerVisible, mPowerMode, mActiveConfig,
  468. mVisibleLayersSortedByZ.size(),
  469. mViewport.left, mViewport.top, mViewport.right, mViewport.bottom,
  470. mFrame.left, mFrame.top, mFrame.right, mFrame.bottom,
  471. mScissor.left, mScissor.top, mScissor.right, mScissor.bottom,
  472. tr[0][0], tr[1][0], tr[2][0],
  473. tr[0][1], tr[1][1], tr[2][1],
  474. tr[0][2], tr[1][2], tr[2][2]);
  475. String8 surfaceDump;
  476. mDisplaySurface->dumpAsString(surfaceDump);
  477. result.append(surfaceDump);
  478. }