Advertisement
ruberval

HWComposer.cpp

Mar 7th, 2013
249
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 47.83 KB | None | 0 0
  1. /*
  2.  * Copyright (C) 2010 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.  
  17. #define ATRACE_TAG ATRACE_TAG_GRAPHICS
  18.  
  19. // Uncomment this to remove support for HWC_DEVICE_API_VERSION_0_3 and older
  20. // #define HWC_REMOVE_DEPRECATED_VERSIONS 1
  21.  
  22. #include <stdint.h>
  23. #include <stdio.h>
  24. #include <stdlib.h>
  25. #include <string.h>
  26. #include <sys/types.h>
  27.  
  28. #include <utils/Errors.h>
  29. #include <utils/misc.h>
  30. #include <utils/String8.h>
  31. #include <utils/Thread.h>
  32. #include <utils/Trace.h>
  33. #include <utils/Vector.h>
  34.  
  35. #include <ui/GraphicBuffer.h>
  36.  
  37. #include <hardware/hardware.h>
  38. #include <hardware/hwcomposer.h>
  39.  
  40. #include <cutils/log.h>
  41. #include <cutils/properties.h>
  42.  
  43. #include "Layer.h"           // needed only for debugging
  44. #include "LayerBase.h"
  45. #include "HWComposer.h"
  46. #include "SurfaceFlinger.h"
  47. #include <utils/CallStack.h>
  48.  
  49. namespace android {
  50.  
  51. // ---------------------------------------------------------------------------
  52. // Support for HWC_DEVICE_API_VERSION_0_3 and older:
  53. // Since v0.3 is deprecated and support will be dropped soon, as much as
  54. // possible the code is written to target v1.0. When using a v0.3 HWC, we
  55. // allocate v0.3 structures, but assign them to v1.0 pointers.
  56.  
  57. #if HWC_REMOVE_DEPRECATED_VERSIONS
  58. // We need complete types to satisfy semantic checks, even though the code
  59. // paths that use these won't get executed at runtime (and will likely be dead-
  60. // code-eliminated). When we remove the code to support v0.3 we can remove
  61. // these as well.
  62. typedef hwc_layer_1_t hwc_layer_t;
  63. typedef hwc_display_contents_1_t hwc_layer_list_t;
  64. typedef hwc_composer_device_1_t hwc_composer_device_t;
  65. #endif
  66.  
  67. // This function assumes we've already rejected HWC's with lower-than-required
  68. // versions. Don't use it for the initial "does HWC meet requirements" check!
  69.  
  70. #define MIN_HWC_HEADER_VERSION 0
  71.  
  72.  
  73. static uint32_t hwcApiVersion(const hwc_composer_device_1_t* hwc) {
  74.     uint32_t hwcVersion = hwc->common.version;
  75.     if (MIN_HWC_HEADER_VERSION == 0 &&
  76.             (hwcVersion & HARDWARE_API_VERSION_2_MAJ_MIN_MASK) == 0) {
  77.         // legacy version encoding
  78.         hwcVersion <<= 16;
  79.     }
  80.     return hwcVersion & HARDWARE_API_VERSION_2_MAJ_MIN_MASK;
  81. }
  82.  
  83. static uint32_t hwcHeaderVersion(const hwc_composer_device_1_t* hwc) {
  84.     uint32_t hwcVersion = hwc->common.version;
  85.     if (MIN_HWC_HEADER_VERSION == 0 &&
  86.             (hwcVersion & HARDWARE_API_VERSION_2_MAJ_MIN_MASK) == 0) {
  87.         // legacy version encoding
  88.         hwcVersion <<= 16;
  89.     }
  90.     return hwcVersion & HARDWARE_API_VERSION_2_HEADER_MASK;
  91. }
  92.  
  93. static bool hwcHasApiVersion(const hwc_composer_device_1_t* hwc,
  94.         uint32_t version) {
  95.     return hwcApiVersion(hwc) >= (version & HARDWARE_API_VERSION_2_MAJ_MIN_MASK);
  96. }
  97.  
  98. static bool hwcHasVsyncEvent(const hwc_composer_device_1_t* hwc) {
  99.     return hwcHasApiVersion(hwc, HWC_DEVICE_API_VERSION_0_3);
  100. }
  101.  
  102. static size_t sizeofHwcLayerList(const hwc_composer_device_1_t* hwc,
  103.         size_t numLayers) {
  104.     if (hwcHasApiVersion(hwc, HWC_DEVICE_API_VERSION_1_0)) {
  105.         return sizeof(hwc_display_contents_1_t) + numLayers*sizeof(hwc_layer_1_t);
  106.     } else {
  107.         return sizeof(hwc_layer_list_t) + numLayers*sizeof(hwc_layer_t);
  108.     }
  109. }
  110.  
  111. static int hwcEventControl(hwc_composer_device_1_t* hwc, int dpy,
  112.         int event, int enabled) {
  113.     if (hwcHasApiVersion(hwc, HWC_DEVICE_API_VERSION_1_0)) {
  114.         return hwc->eventControl(hwc, dpy, event, enabled);
  115.     } else {
  116.         hwc_composer_device_t* hwc0 = reinterpret_cast<hwc_composer_device_t*>(hwc);
  117.         return hwc0->methods->eventControl(hwc0, event, enabled);
  118.     }
  119. }
  120.  
  121. static int hwcBlank(hwc_composer_device_1_t* hwc, int dpy, int blank) {
  122.     if (hwcHasApiVersion(hwc, HWC_DEVICE_API_VERSION_1_0)) {
  123.         return hwc->blank(hwc, dpy, blank);
  124.     } else {
  125.         if (blank) {
  126.             hwc_composer_device_t* hwc0 = reinterpret_cast<hwc_composer_device_t*>(hwc);
  127.             return hwc0->set(hwc0, NULL, NULL, NULL);
  128.         } else {
  129.             // HWC 0.x turns the screen on at the next set()
  130.             return NO_ERROR;
  131.         }
  132.     }
  133. }
  134.  
  135. static int hwcPrepare(hwc_composer_device_1_t* hwc,
  136.         size_t numDisplays, hwc_display_contents_1_t** displays) {
  137.     if (hwcHasApiVersion(hwc, HWC_DEVICE_API_VERSION_1_0)) {
  138.         return hwc->prepare(hwc, numDisplays, displays);
  139.     } else {
  140.         hwc_composer_device_t* hwc0 = reinterpret_cast<hwc_composer_device_t*>(hwc);
  141.         hwc_layer_list_t* list0 = reinterpret_cast<hwc_layer_list_t*>(displays[0]);
  142.         // In the past, SurfaceFlinger would pass a NULL list when doing full
  143.         // OpenGL ES composition. I don't know what, if any, dependencies there
  144.         // are on this behavior, so I'm playing it safe and preserving it.
  145.         // ... and I'm removing it. NULL layers kill the Tegra compositor (RC, Nov 2012)
  146.         /*if (list0->numHwLayers == 0)
  147.             return hwc0->prepare(hwc0, NULL);
  148.         else*/
  149.             return hwc0->prepare(hwc0, list0);
  150.     }
  151. }
  152. static int hwcSet(hwc_composer_device_1_t* hwc, EGLDisplay dpy, EGLSurface sur,
  153.         size_t numDisplays, hwc_display_contents_1_t** displays) {
  154.     int err;
  155.     if (hwcHasApiVersion(hwc, HWC_DEVICE_API_VERSION_1_0)) {
  156.         displays[0]->dpy = dpy;
  157.         displays[0]->sur = sur;
  158.         err = hwc->set(hwc, numDisplays, displays);
  159.     } else {
  160.         hwc_composer_device_t* hwc0 = reinterpret_cast<hwc_composer_device_t*>(hwc);
  161.         hwc_layer_list_t* list0 = reinterpret_cast<hwc_layer_list_t*>(displays[0]);
  162.         err = hwc0->set(hwc0, dpy, sur, list0);
  163.     }
  164.     return err;
  165. }
  166.  
  167. static uint32_t& hwcFlags(hwc_composer_device_1_t* hwc,
  168.         hwc_display_contents_1_t* display) {
  169.     if (hwcHasApiVersion(hwc, HWC_DEVICE_API_VERSION_1_0)) {
  170.         return display->flags;
  171.     } else {
  172.         hwc_layer_list_t* list0 = reinterpret_cast<hwc_layer_list_t*>(display);
  173.         return list0->flags;
  174.     }
  175. }
  176.  
  177. static size_t& hwcNumHwLayers(hwc_composer_device_1_t* hwc,
  178.         hwc_display_contents_1_t* display) {
  179.     if (hwcHasApiVersion(hwc, HWC_DEVICE_API_VERSION_1_0)) {
  180.         return display->numHwLayers;
  181.     } else {
  182.         hwc_layer_list_t* list0 = reinterpret_cast<hwc_layer_list_t*>(display);
  183.         return list0->numHwLayers;
  184.     }
  185. }
  186.  
  187. static void hwcDump(hwc_composer_device_1_t* hwc, char* buff, int buff_len) {
  188.     if (hwcHasApiVersion(hwc, HWC_DEVICE_API_VERSION_1_0)) {
  189.         if (hwc->dump)
  190.             hwc->dump(hwc, buff, buff_len);
  191.     } else if (hwcHasApiVersion(hwc, HWC_DEVICE_API_VERSION_0_1)) {
  192.         hwc_composer_device_t* hwc0 = reinterpret_cast<hwc_composer_device_t*>(hwc);
  193.         if (hwc0->dump)
  194.             hwc0->dump(hwc0, buff, buff_len);
  195.     }
  196. }
  197.  
  198. // ---------------------------------------------------------------------------
  199.  
  200. struct HWComposer::cb_context {
  201.     struct callbacks : public hwc_procs_t {
  202.         // these are here to facilitate the transition when adding
  203.         // new callbacks (an implementation can check for NULL before
  204.         // calling a new callback).
  205.         void (*zero[4])(void);
  206.     };
  207.     callbacks procs;
  208.     HWComposer* hwc;
  209. };
  210.  
  211. // ---------------------------------------------------------------------------
  212.  
  213. HWComposer::HWComposer(
  214.         const sp<SurfaceFlinger>& flinger,
  215.         EventHandler& handler)
  216.     : mFlinger(flinger),
  217.       mFbDev(0), mHwc(0), mNumDisplays(1),
  218.       mCBContext(new cb_context),
  219.       mEventHandler(handler),
  220.       mVSyncCount(0), mDebugForceFakeVSync(false)
  221. {
  222.     for (size_t i =0 ; i<MAX_DISPLAYS ; i++) {
  223.         mLists[i] = 0;
  224.     }
  225.  
  226.     char value[PROPERTY_VALUE_MAX];
  227.     property_get("debug.sf.no_hw_vsync", value, "0");
  228.     mDebugForceFakeVSync = atoi(value);
  229.  
  230.     bool needVSyncThread = true;
  231.  
  232.     // Note: some devices may insist that the FB HAL be opened before HWC.
  233.     loadFbHalModule();
  234.     loadHwcModule();
  235.  
  236.     if (mFbDev && mHwc && hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_1)) {
  237.         // close FB HAL if we don't needed it.
  238.         // FIXME: this is temporary until we're not forced to open FB HAL
  239.         // before HWC.
  240.         framebuffer_close(mFbDev);
  241.         mFbDev = NULL;
  242.     }
  243.  
  244.     // If we have no HWC, or a pre-1.1 HWC, an FB dev is mandatory.
  245.     if ((!mHwc || !hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_1))
  246.             && !mFbDev) {
  247.         ALOGE("ERROR: failed to open framebuffer, aborting");
  248.         abort();
  249.     }
  250.  
  251.     // these display IDs are always reserved
  252.     for (size_t i=0 ; i<HWC_NUM_DISPLAY_TYPES ; i++) {
  253.         mAllocatedDisplayIDs.markBit(i);
  254.     }
  255.  
  256.     if (mHwc) {
  257.         ALOGI("Using %s version %u.%u", HWC_HARDWARE_COMPOSER,
  258.               (hwcApiVersion(mHwc) >> 24) & 0xff,
  259.               (hwcApiVersion(mHwc) >> 16) & 0xff);
  260.         if (hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_0)) {
  261.             if (mHwc->registerProcs) {
  262.                 mCBContext->hwc = this;
  263.                 mCBContext->procs.invalidate = &hook_invalidate;
  264.                 mCBContext->procs.vsync = &hook_vsync;
  265.                 if (hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_1))
  266.                     mCBContext->procs.hotplug = &hook_hotplug;
  267.                 else
  268.                     mCBContext->procs.hotplug = NULL;
  269.                 memset(mCBContext->procs.zero, 0, sizeof(mCBContext->procs.zero));
  270.                 mHwc->registerProcs(mHwc, &mCBContext->procs);
  271.             }
  272.         } else {
  273.             hwc_composer_device_t* hwc0 = reinterpret_cast<hwc_composer_device_t*>(mHwc);
  274.             if (hwc0->registerProcs) {
  275.                 mCBContext->hwc = this;
  276.                 mCBContext->procs.invalidate = &hook_invalidate;
  277.                 mCBContext->procs.vsync = &hook_vsync;
  278.                 memset(mCBContext->procs.zero, 0, sizeof(mCBContext->procs.zero));
  279.                 hwc0->registerProcs(hwc0, &mCBContext->procs);
  280.             }
  281.         }
  282.  
  283.         // don't need a vsync thread if we have a hardware composer
  284.         needVSyncThread = false;
  285.         // always turn vsync off when we start
  286.         if (hwcHasVsyncEvent(mHwc)) {
  287.             eventControl(HWC_DISPLAY_PRIMARY, HWC_EVENT_VSYNC, 0);
  288.  
  289.             // the number of displays we actually have depends on the
  290.             // hw composer version
  291.             if (hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_2)) {
  292.                 // 1.2 adds support for virtual displays
  293.                 mNumDisplays = MAX_DISPLAYS;
  294.             } else if (hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_1)) {
  295.                 // 1.1 adds support for multiple displays
  296.                 mNumDisplays = HWC_NUM_DISPLAY_TYPES;
  297.             } else {
  298.                 mNumDisplays = 1;
  299.             }
  300.         } else {
  301.             needVSyncThread = true;
  302.             mNumDisplays = 1;
  303.         }
  304.     }
  305.  
  306.     if (mFbDev) {
  307.         ALOG_ASSERT(!(mHwc && hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_1)),
  308.                 "should only have fbdev if no hwc or hwc is 1.0");
  309.  
  310.         DisplayData& disp(mDisplayData[HWC_DISPLAY_PRIMARY]);
  311.         disp.connected = true;
  312.         disp.width = mFbDev->width;
  313.         disp.height = mFbDev->height;
  314.         disp.format = mFbDev->format;
  315.         disp.xdpi = mFbDev->xdpi;
  316.         disp.ydpi = mFbDev->ydpi;
  317.         if (disp.refresh == 0) {
  318.             disp.refresh = nsecs_t(1e9 / mFbDev->fps);
  319.             ALOGW("getting VSYNC period from fb HAL: %lld", disp.refresh);
  320.         }
  321.         if (disp.refresh == 0) {
  322.             disp.refresh = nsecs_t(1e9 / 60.0);
  323.             ALOGW("getting VSYNC period from thin air: %lld",
  324.                     mDisplayData[HWC_DISPLAY_PRIMARY].refresh);
  325.         }
  326.     } else if (mHwc) {
  327.         // here we're guaranteed to have at least HWC 1.1
  328.         for (size_t i =0 ; i<HWC_NUM_DISPLAY_TYPES ; i++) {
  329.             queryDisplayProperties(i);
  330.         }
  331.     }
  332.  
  333.     if (needVSyncThread) {
  334.         // we don't have VSYNC support, we need to fake it
  335.         mVSyncThread = new VSyncThread(*this);
  336.     }
  337. }
  338.  
  339. HWComposer::~HWComposer() {
  340.     if (mHwc) {
  341.         eventControl(HWC_DISPLAY_PRIMARY, HWC_EVENT_VSYNC, 0);
  342.     }
  343.     if (mVSyncThread != NULL) {
  344.         mVSyncThread->requestExitAndWait();
  345.     }
  346.     if (mHwc) {
  347.         hwc_close_1(mHwc);
  348.     }
  349.     if (mFbDev) {
  350.         framebuffer_close(mFbDev);
  351.     }
  352.     delete mCBContext;
  353. }
  354.  
  355. // Load and prepare the hardware composer module.  Sets mHwc.
  356. void HWComposer::loadHwcModule()
  357. {
  358.     hw_module_t const* module;
  359.  
  360.     if (hw_get_module(HWC_HARDWARE_MODULE_ID, &module) != 0) {
  361.         ALOGE("%s module not found", HWC_HARDWARE_MODULE_ID);
  362.         return;
  363.     }
  364.  
  365.     int err = hwc_open_1(module, &mHwc);
  366.     if (err) {
  367.         ALOGE("%s device failed to initialize (%s)",
  368.               HWC_HARDWARE_COMPOSER, strerror(-err));
  369.         return;
  370.     }
  371.  
  372.     if (HWC_REMOVE_DEPRECATED_VERSIONS &&
  373.         (!hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_0) ||
  374.             hwcHeaderVersion(mHwc) < MIN_HWC_HEADER_VERSION ||
  375.             hwcHeaderVersion(mHwc) > HWC_HEADER_VERSION)) {
  376.         ALOGE("%s device version %#x unsupported, will not be used",
  377.               HWC_HARDWARE_COMPOSER, mHwc->common.version);
  378.         hwc_close_1(mHwc);
  379.         mHwc = NULL;
  380.         return;
  381.     }
  382. }
  383.  
  384. // Load and prepare the FB HAL, which uses the gralloc module.  Sets mFbDev.
  385. void HWComposer::loadFbHalModule()
  386. {
  387.     hw_module_t const* module;
  388.  
  389.     if (hw_get_module(GRALLOC_HARDWARE_MODULE_ID, &module) != 0) {
  390.         ALOGE("%s module not found", GRALLOC_HARDWARE_MODULE_ID);
  391.         return;
  392.     }
  393.  
  394.     int err = framebuffer_open(module, &mFbDev);
  395.     if (err) {
  396.         ALOGE("framebuffer_open failed (%s)", strerror(-err));
  397.         return;
  398.     }
  399. }
  400.  
  401. status_t HWComposer::initCheck() const {
  402.     return mHwc ? NO_ERROR : NO_INIT;
  403. }
  404.  
  405. void HWComposer::hook_invalidate(const struct hwc_procs* procs) {
  406.     cb_context* ctx = reinterpret_cast<cb_context*>(
  407.             const_cast<hwc_procs_t*>(procs));
  408.     ctx->hwc->invalidate();
  409. }
  410.  
  411. void HWComposer::hook_vsync(const struct hwc_procs* procs, int disp,
  412.         int64_t timestamp) {
  413.     cb_context* ctx = reinterpret_cast<cb_context*>(
  414.             const_cast<hwc_procs_t*>(procs));
  415.     ctx->hwc->vsync(disp, timestamp);
  416. }
  417.  
  418. void HWComposer::hook_hotplug(const struct hwc_procs* procs, int disp,
  419.         int connected) {
  420.     cb_context* ctx = reinterpret_cast<cb_context*>(
  421.             const_cast<hwc_procs_t*>(procs));
  422.     ctx->hwc->hotplug(disp, connected);
  423. }
  424.  
  425. void HWComposer::invalidate() {
  426.     mFlinger->repaintEverything();
  427. }
  428.  
  429. void HWComposer::vsync(int disp, int64_t timestamp) {
  430.     ATRACE_INT("VSYNC", ++mVSyncCount&1);
  431.     mEventHandler.onVSyncReceived(disp, timestamp);
  432.     Mutex::Autolock _l(mLock);
  433.     mLastHwVSync = timestamp;
  434. }
  435.  
  436. void HWComposer::hotplug(int disp, int connected) {
  437.     if (disp == HWC_DISPLAY_PRIMARY || disp >= HWC_NUM_DISPLAY_TYPES) {
  438.         ALOGE("hotplug event received for invalid display: disp=%d connected=%d",
  439.                 disp, connected);
  440.         return;
  441.     }
  442.     queryDisplayProperties(disp);
  443.     mEventHandler.onHotplugReceived(disp, bool(connected));
  444. }
  445.  
  446. static const uint32_t DISPLAY_ATTRIBUTES[] = {
  447.     HWC_DISPLAY_VSYNC_PERIOD,
  448.     HWC_DISPLAY_WIDTH,
  449.     HWC_DISPLAY_HEIGHT,
  450.     HWC_DISPLAY_DPI_X,
  451.     HWC_DISPLAY_DPI_Y,
  452.     HWC_DISPLAY_NO_ATTRIBUTE,
  453. };
  454. #define NUM_DISPLAY_ATTRIBUTES (sizeof(DISPLAY_ATTRIBUTES) / sizeof(DISPLAY_ATTRIBUTES)[0])
  455.  
  456. // http://developer.android.com/reference/android/util/DisplayMetrics.html
  457. #define ANDROID_DENSITY_TV    213
  458. #define ANDROID_DENSITY_XHIGH 320
  459.  
  460. status_t HWComposer::queryDisplayProperties(int disp) {
  461.  
  462.     LOG_ALWAYS_FATAL_IF(!mHwc || !hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_1));
  463.  
  464.     // use zero as default value for unspecified attributes
  465.     int32_t values[NUM_DISPLAY_ATTRIBUTES - 1];
  466.     memset(values, 0, sizeof(values));
  467.  
  468.     uint32_t config;
  469.     size_t numConfigs = 1;
  470.     status_t err = mHwc->getDisplayConfigs(mHwc, disp, &config, &numConfigs);
  471.     if (err != NO_ERROR) {
  472.         // this can happen if an unpluggable display is not connected
  473.         mDisplayData[disp].connected = false;
  474.         return err;
  475.     }
  476.  
  477.     err = mHwc->getDisplayAttributes(mHwc, disp, config, DISPLAY_ATTRIBUTES, values);
  478.     if (err != NO_ERROR) {
  479.         // we can't get this display's info. turn it off.
  480.         mDisplayData[disp].connected = false;
  481.         return err;
  482.     }
  483.  
  484.     int32_t w = 0, h = 0;
  485.     for (size_t i = 0; i < NUM_DISPLAY_ATTRIBUTES - 1; i++) {
  486.         switch (DISPLAY_ATTRIBUTES[i]) {
  487.         case HWC_DISPLAY_VSYNC_PERIOD:
  488.             mDisplayData[disp].refresh = nsecs_t(values[i]);
  489.             break;
  490.         case HWC_DISPLAY_WIDTH:
  491.             mDisplayData[disp].width = values[i];
  492.             break;
  493.         case HWC_DISPLAY_HEIGHT:
  494.             mDisplayData[disp].height = values[i];
  495.             break;
  496.         case HWC_DISPLAY_DPI_X:
  497.             mDisplayData[disp].xdpi = values[i] / 1000.0f;
  498.             break;
  499.         case HWC_DISPLAY_DPI_Y:
  500.             mDisplayData[disp].ydpi = values[i] / 1000.0f;
  501.             break;
  502.         default:
  503.             ALOG_ASSERT(false, "unknown display attribute[%d] %#x",
  504.                     i, DISPLAY_ATTRIBUTES[i]);
  505.             break;
  506.         }
  507.     }
  508.  
  509.     // FIXME: what should we set the format to?
  510.     mDisplayData[disp].format = HAL_PIXEL_FORMAT_RGBA_8888;
  511.     mDisplayData[disp].connected = true;
  512.     if (mDisplayData[disp].xdpi == 0.0f || mDisplayData[disp].ydpi == 0.0f) {
  513.         // is there anything smarter we can do?
  514.         if (h >= 1080) {
  515.             mDisplayData[disp].xdpi = ANDROID_DENSITY_XHIGH;
  516.             mDisplayData[disp].ydpi = ANDROID_DENSITY_XHIGH;
  517.         } else {
  518.             mDisplayData[disp].xdpi = ANDROID_DENSITY_TV;
  519.             mDisplayData[disp].ydpi = ANDROID_DENSITY_TV;
  520.         }
  521.     }
  522.     return NO_ERROR;
  523. }
  524.  
  525. int32_t HWComposer::allocateDisplayId() {
  526.     if (mAllocatedDisplayIDs.count() >= mNumDisplays) {
  527.         return NO_MEMORY;
  528.     }
  529.     int32_t id = mAllocatedDisplayIDs.firstUnmarkedBit();
  530.     mAllocatedDisplayIDs.markBit(id);
  531.     return id;
  532. }
  533.  
  534. status_t HWComposer::freeDisplayId(int32_t id) {
  535.     if (id < HWC_NUM_DISPLAY_TYPES) {
  536.         // cannot free the reserved IDs
  537.         return BAD_VALUE;
  538.     }
  539.     if (uint32_t(id)>31 || !mAllocatedDisplayIDs.hasBit(id)) {
  540.         return BAD_INDEX;
  541.     }
  542.     mAllocatedDisplayIDs.clearBit(id);
  543.     return NO_ERROR;
  544. }
  545.  
  546. nsecs_t HWComposer::getRefreshPeriod(int disp) const {
  547.     return mDisplayData[disp].refresh;
  548. }
  549.  
  550. nsecs_t HWComposer::getRefreshTimestamp(int disp) const {
  551.     // this returns the last refresh timestamp.
  552.     // if the last one is not available, we estimate it based on
  553.     // the refresh period and whatever closest timestamp we have.
  554.     Mutex::Autolock _l(mLock);
  555.     nsecs_t now = systemTime(CLOCK_MONOTONIC);
  556.     return now - ((now - mLastHwVSync) %  mDisplayData[disp].refresh);
  557. }
  558.  
  559. uint32_t HWComposer::getWidth(int disp) const {
  560.     return mDisplayData[disp].width;
  561. }
  562.  
  563. uint32_t HWComposer::getHeight(int disp) const {
  564.     return mDisplayData[disp].height;
  565. }
  566.  
  567. uint32_t HWComposer::getFormat(int disp) const {
  568.     return mDisplayData[disp].format;
  569. }
  570.  
  571. float HWComposer::getDpiX(int disp) const {
  572.     return mDisplayData[disp].xdpi;
  573. }
  574.  
  575. float HWComposer::getDpiY(int disp) const {
  576.     return mDisplayData[disp].ydpi;
  577. }
  578.  
  579. bool HWComposer::isConnected(int disp) const {
  580.     return mDisplayData[disp].connected;
  581. }
  582.  
  583. void HWComposer::eventControl(int disp, int event, int enabled) {
  584.     if (uint32_t(disp)>31 || !mAllocatedDisplayIDs.hasBit(disp)) {
  585.         ALOGD("eventControl ignoring event %d on unallocated disp %d (en=%d)",
  586.               event, disp, enabled);
  587.         return;
  588.     }
  589.     if (event != EVENT_VSYNC) {
  590.         ALOGW("eventControl got unexpected event %d (disp=%d en=%d)",
  591.               event, disp, enabled);
  592.         return;
  593.     }
  594.     status_t err = NO_ERROR;
  595.     if (mHwc && !mDebugForceFakeVSync && hwcHasVsyncEvent(mHwc))  {
  596.         if (hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_0)) {
  597.             // NOTE: we use our own internal lock here because we have to call
  598.             // into the HWC with the lock held, and we want to make sure
  599.             // that even if HWC blocks (which it shouldn't), it won't
  600.             // affect other threads.
  601.             Mutex::Autolock _l(mEventControlLock);
  602.             const int32_t eventBit = 1UL << event;
  603.             const int32_t newValue = enabled ? eventBit : 0;
  604.             const int32_t oldValue = mDisplayData[disp].events & eventBit;
  605.             if (newValue != oldValue) {
  606.                 ATRACE_CALL();
  607.                 err = hwcEventControl(mHwc, disp, event, enabled);
  608.                 if (!err) {
  609.                     int32_t& events(mDisplayData[disp].events);
  610.                     events = (events & ~eventBit) | newValue;
  611.                 }
  612.             }
  613.         } else {
  614.             err = hwcEventControl(mHwc, disp, event, enabled);
  615.         }
  616.         // error here should not happen -- not sure what we should
  617.         // do if it does.
  618.         ALOGE_IF(err, "eventControl(%d, %d) failed %s",
  619.                 event, enabled, strerror(-err));
  620.     }
  621.  
  622.     if (err == NO_ERROR && mVSyncThread != NULL) {
  623.         mVSyncThread->setEnabled(enabled);
  624.     }
  625. }
  626.  
  627. status_t HWComposer::createWorkList(int32_t id, size_t numLayers) {
  628.     if (uint32_t(id)>31 || !mAllocatedDisplayIDs.hasBit(id)) {
  629.         return BAD_INDEX;
  630.     }
  631.  
  632.     if (mHwc) {
  633.         DisplayData& disp(mDisplayData[id]);
  634.         if (hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_1)) {
  635.             // we need space for the HWC_FRAMEBUFFER_TARGET
  636.             numLayers++;
  637.         }
  638.         if (disp.capacity < numLayers || disp.list == NULL) {
  639.             size_t size = sizeofHwcLayerList(mHwc, numLayers);
  640.             free(disp.list);
  641.             disp.list = (hwc_display_contents_1_t*)malloc(size);
  642.             disp.capacity = numLayers;
  643.         }
  644.         if (hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_1)) {
  645.             disp.framebufferTarget = &disp.list->hwLayers[numLayers - 1];
  646.             memset(disp.framebufferTarget, 0, sizeof(hwc_layer_1_t));
  647.             const hwc_rect_t r = { 0, 0, disp.width, disp.height };
  648.             disp.framebufferTarget->compositionType = HWC_FRAMEBUFFER_TARGET;
  649.             disp.framebufferTarget->hints = 0;
  650.             disp.framebufferTarget->flags = 0;
  651.             disp.framebufferTarget->handle = disp.fbTargetHandle;
  652.             disp.framebufferTarget->transform = 0;
  653.             disp.framebufferTarget->blending = HWC_BLENDING_PREMULT;
  654.             disp.framebufferTarget->sourceCrop = r;
  655.             disp.framebufferTarget->displayFrame = r;
  656.             disp.framebufferTarget->visibleRegionScreen.numRects = 1;
  657.             disp.framebufferTarget->visibleRegionScreen.rects =
  658.                 &disp.framebufferTarget->displayFrame;
  659.             disp.framebufferTarget->acquireFenceFd = -1;
  660.             disp.framebufferTarget->releaseFenceFd = -1;
  661.         }
  662.         if (hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_0)) {
  663.             disp.list->retireFenceFd = -1;
  664.         }
  665.         hwcFlags(mHwc, disp.list) = HWC_GEOMETRY_CHANGED;
  666.         hwcNumHwLayers(mHwc, disp.list) = numLayers;
  667.     }
  668.     return NO_ERROR;
  669. }
  670.  
  671. status_t HWComposer::setFramebufferTarget(int32_t id,
  672.         const sp<Fence>& acquireFence, const sp<GraphicBuffer>& buf) {
  673.     if (uint32_t(id)>31 || !mAllocatedDisplayIDs.hasBit(id)) {
  674.         return BAD_INDEX;
  675.     }
  676.     DisplayData& disp(mDisplayData[id]);
  677.     if (!disp.framebufferTarget) {
  678.         // this should never happen, but apparently eglCreateWindowSurface()
  679.         // triggers a SurfaceTextureClient::queueBuffer()  on some
  680.         // devices (!?) -- log and ignore.
  681.         ALOGE("HWComposer: framebufferTarget is null");
  682. //        CallStack stack;
  683. //        stack.update();
  684. //        stack.dump("");
  685.         return NO_ERROR;
  686.     }
  687.  
  688.     int acquireFenceFd = -1;
  689.     if (acquireFence != NULL) {
  690.         acquireFenceFd = acquireFence->dup();
  691.     }
  692.  
  693.     // ALOGD("fbPost: handle=%p, fence=%d", buf->handle, acquireFenceFd);
  694.     disp.fbTargetHandle = buf->handle;
  695.     disp.framebufferTarget->handle = disp.fbTargetHandle;
  696.     disp.framebufferTarget->acquireFenceFd = acquireFenceFd;
  697.     return NO_ERROR;
  698. }
  699.  
  700. status_t HWComposer::prepare() {
  701.     for (size_t i=0 ; i<mNumDisplays ; i++) {
  702.         DisplayData& disp(mDisplayData[i]);
  703.         if (disp.framebufferTarget) {
  704.             // make sure to reset the type to HWC_FRAMEBUFFER_TARGET
  705.             // DO NOT reset the handle field to NULL, because it's possible
  706.             // that we have nothing to redraw (eg: eglSwapBuffers() not called)
  707.             // in which case, we should continue to use the same buffer.
  708.             LOG_FATAL_IF(disp.list == NULL);
  709.             disp.framebufferTarget->compositionType = HWC_FRAMEBUFFER_TARGET;
  710.         }
  711.         if (!disp.connected && disp.list != NULL) {
  712.             ALOGW("WARNING: disp %d: connected, non-null list, layers=%d",
  713.                     i, hwcNumHwLayers(mHwc, disp.list));
  714.         }
  715.         mLists[i] = disp.list;
  716.         if (mLists[i]) {
  717.             if (hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_2)) {
  718.                 mLists[i]->outbuf = NULL;
  719.                 mLists[i]->outbufAcquireFenceFd = -1;
  720.             } else if (hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_1)) {
  721.                 // garbage data to catch improper use
  722.                 mLists[i]->dpy = (hwc_display_t)0xDEADBEEF;
  723.                 mLists[i]->sur = (hwc_surface_t)0xDEADBEEF;
  724.             } else if (hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_0)) {
  725.                 mLists[i]->dpy = EGL_NO_DISPLAY;
  726.                 mLists[i]->sur = EGL_NO_SURFACE;
  727.             }
  728.         }
  729.     }
  730.     int err = hwcPrepare(mHwc, mNumDisplays, mLists);
  731.     ALOGE_IF(err, "HWComposer: prepare failed (%s)", strerror(-err));
  732.  
  733.     if (err == NO_ERROR) {
  734.         if (hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_0)) {
  735.             // here we're just making sure that "skip" layers are set
  736.             // to HWC_FRAMEBUFFER and we're also counting how many layers
  737.             // we have of each type.
  738.             for (size_t i=0 ; i<mNumDisplays ; i++) {
  739.                 DisplayData& disp(mDisplayData[i]);
  740.                 disp.hasFbComp = false;
  741.                 disp.hasOvComp = false;
  742.                 if (disp.list) {
  743.                     for (size_t i=0 ; i<hwcNumHwLayers(mHwc, disp.list) ; i++) {
  744.                         hwc_layer_1_t& l = disp.list->hwLayers[i];
  745.  
  746.                         //ALOGD("prepare: %d, type=%d, handle=%p",
  747.                         //        i, l.compositionType, l.handle);
  748.  
  749.                         if (l.flags & HWC_SKIP_LAYER) {
  750.                             l.compositionType = HWC_FRAMEBUFFER;
  751.                         }
  752.                         if (l.compositionType == HWC_FRAMEBUFFER) {
  753.                             disp.hasFbComp = true;
  754.                         }
  755.                         if (l.compositionType == HWC_OVERLAY) {
  756.                             disp.hasOvComp = true;
  757.                         }
  758.                     }
  759.                 }
  760.             }
  761.         } else {
  762.             DisplayData& disp(mDisplayData[0]);
  763.             disp.hasFbComp = false;
  764.             disp.hasOvComp = false;
  765.             if (disp.list) {
  766.                 hwc_layer_list_t* list0 = reinterpret_cast<hwc_layer_list_t*>(disp.list);
  767.                 for (size_t i=0 ; i<hwcNumHwLayers(mHwc, disp.list) ; i++) {
  768.                     hwc_layer_t& l = list0->hwLayers[i];
  769.  
  770.                     //ALOGD("prepare: %d, type=%d, handle=%p",
  771.                     //        i, l.compositionType, l.handle);
  772.  
  773.                     if (l.flags & HWC_SKIP_LAYER) {
  774.                         l.compositionType = HWC_FRAMEBUFFER;
  775.                     }
  776.                     if (l.compositionType == HWC_FRAMEBUFFER) {
  777.                         disp.hasFbComp = true;
  778.                     }
  779.                     // If the composition type is BLIT, we set this to
  780.                     // trigger a FLIP
  781.                     if(l.compositionType == HWC_BLIT) {
  782.                         disp.hasFbComp = true;
  783.                     }
  784.                     if (l.compositionType == HWC_OVERLAY) {
  785.                         disp.hasOvComp = true;
  786.                     }
  787.                 }
  788.             }
  789.  
  790.         }
  791.  
  792.     }
  793.     return (status_t)err;
  794. }
  795.  
  796. bool HWComposer::hasHwcComposition(int32_t id) const {
  797.     if (uint32_t(id)>31 || !mAllocatedDisplayIDs.hasBit(id))
  798.         return false;
  799.     return mDisplayData[id].hasOvComp;
  800. }
  801.  
  802. bool HWComposer::hasGlesComposition(int32_t id) const {
  803.     if (uint32_t(id)>31 || !mAllocatedDisplayIDs.hasBit(id))
  804.         return false;
  805.     return mDisplayData[id].hasFbComp;
  806. }
  807.  
  808. int HWComposer::getAndResetReleaseFenceFd(int32_t id) {
  809.     if (uint32_t(id)>31 || !mAllocatedDisplayIDs.hasBit(id))
  810.         return BAD_INDEX;
  811.  
  812.     int fd = INVALID_OPERATION;
  813.     if (mHwc && hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_1)) {
  814.         const DisplayData& disp(mDisplayData[id]);
  815.         if (disp.framebufferTarget) {
  816.             fd = disp.framebufferTarget->releaseFenceFd;
  817.             disp.framebufferTarget->acquireFenceFd = -1;
  818.             disp.framebufferTarget->releaseFenceFd = -1;
  819.         }
  820.     }
  821.     return fd;
  822. }
  823.  
  824. status_t HWComposer::commit() {
  825.     int err = NO_ERROR;
  826.     if (mHwc) {
  827.         if (hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_0)) {
  828.             if (!hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_1)) {
  829.                 // On version 1.0, the OpenGL ES target surface is communicated
  830.                 // by the (dpy, sur) fields and we are guaranteed to have only
  831.                 // a single display.
  832.                 mLists[0]->dpy = eglGetCurrentDisplay();
  833.                 mLists[0]->sur = eglGetCurrentSurface(EGL_DRAW);
  834.             }
  835.             err = hwcSet(mHwc, mLists[0]->dpy, mLists[0]->sur, mNumDisplays,
  836.                     const_cast<hwc_display_contents_1_t**>(mLists));
  837.         } else {
  838.             err = hwcSet(mHwc, eglGetCurrentDisplay(), eglGetCurrentSurface(EGL_DRAW), mNumDisplays,
  839.                     const_cast<hwc_display_contents_1_t**>(mLists));
  840.         }
  841.  
  842.  
  843.         for (size_t i=0 ; i<mNumDisplays ; i++) {
  844.             DisplayData& disp(mDisplayData[i]);
  845.             if (disp.list) {
  846.                 if (hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_0)) {
  847.                     if (disp.list->retireFenceFd != -1) {
  848.                         close(disp.list->retireFenceFd);
  849.                         disp.list->retireFenceFd = -1;
  850.                     }
  851.                 }
  852.                 hwcFlags(mHwc, disp.list) &= ~HWC_GEOMETRY_CHANGED;
  853.             }
  854.         }
  855.     }
  856.     return (status_t)err;
  857. }
  858.  
  859. status_t HWComposer::release(int disp) {
  860.     LOG_FATAL_IF(disp >= HWC_NUM_DISPLAY_TYPES);
  861.     if (mHwc) {
  862.         if (hwcHasVsyncEvent(mHwc)) {
  863.             eventControl(disp, HWC_EVENT_VSYNC, 0);
  864.         }
  865.         return (status_t)hwcBlank(mHwc, disp, 1);
  866.     }
  867.     return NO_ERROR;
  868. }
  869.  
  870. status_t HWComposer::acquire(int disp) {
  871.     LOG_FATAL_IF(disp >= HWC_NUM_DISPLAY_TYPES);
  872.     if (mHwc) {
  873.         return (status_t)hwcBlank(mHwc, disp, 0);
  874.     }
  875.     return NO_ERROR;
  876. }
  877.  
  878. void HWComposer::disconnectDisplay(int disp) {
  879.     LOG_ALWAYS_FATAL_IF(disp < 0 || disp == HWC_DISPLAY_PRIMARY);
  880.     if (disp >= HWC_NUM_DISPLAY_TYPES) {
  881.         // nothing to do for these yet
  882.         return;
  883.     }
  884.     DisplayData& dd(mDisplayData[disp]);
  885.     if (dd.list != NULL) {
  886.         free(dd.list);
  887.         dd.list = NULL;
  888.         dd.framebufferTarget = NULL;    // points into dd.list
  889.         dd.fbTargetHandle = NULL;
  890.     }
  891. }
  892.  
  893. int HWComposer::getVisualID() const {
  894.     if (mHwc && hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_1)) {
  895.         // FIXME: temporary hack until HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED
  896.         // is supported by the implementation. we can only be in this case
  897.         // if we have HWC 1.1
  898.         return HAL_PIXEL_FORMAT_RGBA_8888;
  899.         //return HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED;
  900.     } else {
  901.         return mFbDev->format;
  902.     }
  903. }
  904.  
  905. bool HWComposer::supportsFramebufferTarget() const {
  906.     return (mHwc && hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_1));
  907. }
  908.  
  909. int HWComposer::fbPost(int32_t id,
  910.         const sp<Fence>& acquireFence, const sp<GraphicBuffer>& buffer) {
  911.     if (mHwc && hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_1)) {
  912.         return setFramebufferTarget(id, acquireFence, buffer);
  913.     } else {
  914.         if (acquireFence != NULL) {
  915.             acquireFence->waitForever(1000, "HWComposer::fbPost");
  916.         }
  917.         return mFbDev->post(mFbDev, buffer->handle);
  918.     }
  919. }
  920.  
  921. int HWComposer::fbCompositionComplete() {
  922.     if (mHwc && hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_1))
  923.         return NO_ERROR;
  924.  
  925.     if (mFbDev->compositionComplete) {
  926.         return mFbDev->compositionComplete(mFbDev);
  927.     } else {
  928.         return INVALID_OPERATION;
  929.     }
  930. }
  931.  
  932. void HWComposer::fbDump(String8& result) {
  933.     if (mFbDev && mFbDev->common.version >= 1 && mFbDev->dump) {
  934.         const size_t SIZE = 4096;
  935.         char buffer[SIZE];
  936.         mFbDev->dump(mFbDev, buffer, SIZE);
  937.         result.append(buffer);
  938.     }
  939. }
  940.  
  941. /*
  942.  * Helper template to implement a concrete HWCLayer
  943.  * This holds the pointer to the concrete hwc layer type
  944.  * and implements the "iterable" side of HWCLayer.
  945.  */
  946. template<typename CONCRETE, typename HWCTYPE>
  947. class Iterable : public HWComposer::HWCLayer {
  948. protected:
  949.     HWCTYPE* const mLayerList;
  950.     HWCTYPE* mCurrentLayer;
  951.     Iterable(HWCTYPE* layer) : mLayerList(layer), mCurrentLayer(layer) { }
  952.     inline HWCTYPE const * getLayer() const { return mCurrentLayer; }
  953.     inline HWCTYPE* getLayer() { return mCurrentLayer; }
  954.     virtual ~Iterable() { }
  955. private:
  956.     // returns a copy of ourselves
  957.     virtual HWComposer::HWCLayer* dup() {
  958.         return new CONCRETE( static_cast<const CONCRETE&>(*this) );
  959.     }
  960.     virtual status_t setLayer(size_t index) {
  961.         mCurrentLayer = &mLayerList[index];
  962.         return NO_ERROR;
  963.     }
  964. };
  965.  
  966. // #if !HWC_REMOVE_DEPRECATED_VERSIONS
  967. /*
  968.  * Concrete implementation of HWCLayer for HWC_DEVICE_API_VERSION_0_3
  969.  * This implements the HWCLayer side of HWCIterableLayer.
  970.  */
  971. class HWCLayerVersion0 : public Iterable<HWCLayerVersion0, hwc_layer_t> {
  972. public:
  973.     HWCLayerVersion0(hwc_layer_t* layer)
  974.         : Iterable<HWCLayerVersion0, hwc_layer_t>(layer) { }
  975.  
  976.     virtual int32_t getCompositionType() const {
  977.         return getLayer()->compositionType;
  978.     }
  979.     virtual uint32_t getHints() const {
  980.         return getLayer()->hints;
  981.     }
  982.     virtual int getAndResetReleaseFenceFd() {
  983.         // not supported on VERSION_03
  984.         return -1;
  985.     }
  986.     virtual void setAcquireFenceFd(int fenceFd) {
  987.         if (fenceFd != -1) {
  988.             ALOGE("HWC 0.x can't handle acquire fences");
  989.             close(fenceFd);
  990.         }
  991.     }
  992.  
  993.     virtual void setDefaultState() {
  994.         getLayer()->compositionType = HWC_FRAMEBUFFER;
  995.         getLayer()->hints = 0;
  996.         getLayer()->flags = HWC_SKIP_LAYER;
  997.         getLayer()->handle = 0;
  998.         getLayer()->transform = 0;
  999.         getLayer()->blending = HWC_BLENDING_NONE;
  1000.         getLayer()->visibleRegionScreen.numRects = 0;
  1001.         getLayer()->visibleRegionScreen.rects = NULL;
  1002.     }
  1003.     virtual void setPerFrameDefaultState() {
  1004.         //getLayer()->compositionType = HWC_FRAMEBUFFER;
  1005.     }
  1006.     virtual void setSkip(bool skip) {
  1007.         if (skip) {
  1008.             getLayer()->flags |= HWC_SKIP_LAYER;
  1009.         } else {
  1010.             getLayer()->flags &= ~HWC_SKIP_LAYER;
  1011.         }
  1012.     }
  1013.     virtual void setBlending(uint32_t blending) {
  1014.         getLayer()->blending = blending;
  1015.     }
  1016.     virtual void setTransform(uint32_t transform) {
  1017.         getLayer()->transform = transform;
  1018.     }
  1019.     virtual void setFrame(const Rect& frame) {
  1020.         reinterpret_cast<Rect&>(getLayer()->displayFrame) = frame;
  1021.     }
  1022.     virtual void setCrop(const Rect& crop) {
  1023.         reinterpret_cast<Rect&>(getLayer()->sourceCrop) = crop;
  1024.     }
  1025.     virtual void setVisibleRegionScreen(const Region& reg) {
  1026.         // Region::getSharedBuffer creates a reference to the underlying
  1027.         // SharedBuffer of this Region, this reference is freed
  1028.         // in onDisplayed()
  1029.         hwc_region_t& visibleRegion = getLayer()->visibleRegionScreen;
  1030.         SharedBuffer const* sb = reg.getSharedBuffer(&visibleRegion.numRects);
  1031.         visibleRegion.rects = reinterpret_cast<hwc_rect_t const *>(sb->data());
  1032.     }
  1033.     virtual void setBuffer(const sp<GraphicBuffer>& buffer) {
  1034.         if (buffer == 0 || buffer->handle == 0) {
  1035.             getLayer()->compositionType = HWC_FRAMEBUFFER;
  1036.             getLayer()->flags |= HWC_SKIP_LAYER;
  1037.             getLayer()->handle = 0;
  1038.         } else {
  1039.             getLayer()->handle = buffer->handle;
  1040.         }
  1041.     }
  1042.     virtual void onDisplayed() {
  1043.         hwc_region_t& visibleRegion = getLayer()->visibleRegionScreen;
  1044.         SharedBuffer const* sb = SharedBuffer::bufferFromData(visibleRegion.rects);
  1045.         if (sb) {
  1046.             sb->release();
  1047.             // not technically needed but safer
  1048.             visibleRegion.numRects = 0;
  1049.             visibleRegion.rects = NULL;
  1050.         }
  1051.  
  1052.     }
  1053. };
  1054. // #endif // !HWC_REMOVE_DEPRECATED_VERSIONS
  1055.  
  1056. /*
  1057.  * Concrete implementation of HWCLayer for HWC_DEVICE_API_VERSION_1_0.
  1058.  * This implements the HWCLayer side of HWCIterableLayer.
  1059.  */
  1060. class HWCLayerVersion1 : public Iterable<HWCLayerVersion1, hwc_layer_1_t> {
  1061. public:
  1062.     HWCLayerVersion1(hwc_layer_1_t* layer)
  1063.         : Iterable<HWCLayerVersion1, hwc_layer_1_t>(layer) { }
  1064.  
  1065.     virtual int32_t getCompositionType() const {
  1066.         return getLayer()->compositionType;
  1067.     }
  1068.     virtual uint32_t getHints() const {
  1069.         return getLayer()->hints;
  1070.     }
  1071.     virtual int getAndResetReleaseFenceFd() {
  1072.         int fd = getLayer()->releaseFenceFd;
  1073.         getLayer()->releaseFenceFd = -1;
  1074.         return fd;
  1075.     }
  1076.     virtual void setAcquireFenceFd(int fenceFd) {
  1077.         getLayer()->acquireFenceFd = fenceFd;
  1078.     }
  1079.     virtual void setPerFrameDefaultState() {
  1080.         //getLayer()->compositionType = HWC_FRAMEBUFFER;
  1081.     }
  1082.     virtual void setDefaultState() {
  1083.         getLayer()->compositionType = HWC_FRAMEBUFFER;
  1084.         getLayer()->hints = 0;
  1085.         getLayer()->flags = HWC_SKIP_LAYER;
  1086.         getLayer()->handle = 0;
  1087.         getLayer()->transform = 0;
  1088.         getLayer()->blending = HWC_BLENDING_NONE;
  1089.         getLayer()->visibleRegionScreen.numRects = 0;
  1090.         getLayer()->visibleRegionScreen.rects = NULL;
  1091.         getLayer()->acquireFenceFd = -1;
  1092.         getLayer()->releaseFenceFd = -1;
  1093.     }
  1094.     virtual void setSkip(bool skip) {
  1095.         if (skip) {
  1096.             getLayer()->flags |= HWC_SKIP_LAYER;
  1097.         } else {
  1098.             getLayer()->flags &= ~HWC_SKIP_LAYER;
  1099.         }
  1100.     }
  1101.     virtual void setBlending(uint32_t blending) {
  1102.         getLayer()->blending = blending;
  1103.     }
  1104.     virtual void setTransform(uint32_t transform) {
  1105.         getLayer()->transform = transform;
  1106.     }
  1107.     virtual void setFrame(const Rect& frame) {
  1108.         reinterpret_cast<Rect&>(getLayer()->displayFrame) = frame;
  1109.     }
  1110.     virtual void setCrop(const Rect& crop) {
  1111.         reinterpret_cast<Rect&>(getLayer()->sourceCrop) = crop;
  1112.     }
  1113.     virtual void setVisibleRegionScreen(const Region& reg) {
  1114.         // Region::getSharedBuffer creates a reference to the underlying
  1115.         // SharedBuffer of this Region, this reference is freed
  1116.         // in onDisplayed()
  1117.         hwc_region_t& visibleRegion = getLayer()->visibleRegionScreen;
  1118.         SharedBuffer const* sb = reg.getSharedBuffer(&visibleRegion.numRects);
  1119.         visibleRegion.rects = reinterpret_cast<hwc_rect_t const *>(sb->data());
  1120.     }
  1121.     virtual void setBuffer(const sp<GraphicBuffer>& buffer) {
  1122.         if (buffer == 0 || buffer->handle == 0) {
  1123.             getLayer()->compositionType = HWC_FRAMEBUFFER;
  1124.             getLayer()->flags |= HWC_SKIP_LAYER;
  1125.             getLayer()->handle = 0;
  1126.         } else {
  1127.             getLayer()->handle = buffer->handle;
  1128.         }
  1129.     }
  1130.     virtual void onDisplayed() {
  1131.         hwc_region_t& visibleRegion = getLayer()->visibleRegionScreen;
  1132.         SharedBuffer const* sb = SharedBuffer::bufferFromData(visibleRegion.rects);
  1133.         if (sb) {
  1134.             sb->release();
  1135.             // not technically needed but safer
  1136.             visibleRegion.numRects = 0;
  1137.             visibleRegion.rects = NULL;
  1138.         }
  1139.  
  1140.         getLayer()->acquireFenceFd = -1;
  1141.     }
  1142. };
  1143.  
  1144. /*
  1145.  * returns an iterator initialized at a given index in the layer list
  1146.  */
  1147. HWComposer::LayerListIterator HWComposer::getLayerIterator(int32_t id, size_t index) {
  1148.     if (uint32_t(id)>31 || !mAllocatedDisplayIDs.hasBit(id)) {
  1149.         return LayerListIterator();
  1150.     }
  1151.     const DisplayData& disp(mDisplayData[id]);
  1152.     if (!mHwc || !disp.list || index > hwcNumHwLayers(mHwc,disp.list)) {
  1153.         return LayerListIterator();
  1154.     }
  1155.     if (hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_0)) {
  1156.        return LayerListIterator(new HWCLayerVersion1(disp.list->hwLayers), index);
  1157.     } else {
  1158.        hwc_layer_list_t* list0 = reinterpret_cast<hwc_layer_list_t*>(disp.list);
  1159.        return LayerListIterator(new HWCLayerVersion0(list0->hwLayers), index);
  1160.     }
  1161. }
  1162.  
  1163. /*
  1164.  * returns an iterator on the beginning of the layer list
  1165.  */
  1166. HWComposer::LayerListIterator HWComposer::begin(int32_t id) {
  1167.     return getLayerIterator(id, 0);
  1168. }
  1169.  
  1170. /*
  1171.  * returns an iterator on the end of the layer list
  1172.  */
  1173. HWComposer::LayerListIterator HWComposer::end(int32_t id) {
  1174.     size_t numLayers = 0;
  1175.     if (uint32_t(id) <= 31 && mAllocatedDisplayIDs.hasBit(id)) {
  1176.         const DisplayData& disp(mDisplayData[id]);
  1177.         if (mHwc && disp.list) {
  1178.             numLayers = hwcNumHwLayers(mHwc, disp.list);
  1179.             if (hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_1)) {
  1180.                 // with HWC 1.1, the last layer is always the HWC_FRAMEBUFFER_TARGET,
  1181.                 // which we ignore when iterating through the layer list.
  1182.                 ALOGE_IF(!numLayers, "mDisplayData[%d].list->numHwLayers is 0", id);
  1183.                 if (numLayers) {
  1184.                     numLayers--;
  1185.                 }
  1186.             }
  1187.         }
  1188.     }
  1189.     return getLayerIterator(id, numLayers);
  1190. }
  1191.  
  1192. void HWComposer::dump(String8& result, char* buffer, size_t SIZE) const {
  1193.     if (mHwc) {
  1194.         result.appendFormat("Hardware Composer state (version %8x):\n", hwcApiVersion(mHwc));
  1195.         result.appendFormat("  mDebugForceFakeVSync=%d\n", mDebugForceFakeVSync);
  1196.         for (size_t i=0 ; i<mNumDisplays ; i++) {
  1197.             const DisplayData& disp(mDisplayData[i]);
  1198.  
  1199.             const Vector< sp<LayerBase> >& visibleLayersSortedByZ =
  1200.                     mFlinger->getLayerSortedByZForHwcDisplay(i);
  1201.  
  1202.             if (disp.connected) {
  1203.                 result.appendFormat(
  1204.                         "  Display[%d] : %ux%u, xdpi=%f, ydpi=%f, refresh=%lld\n",
  1205.                         i, disp.width, disp.height, disp.xdpi, disp.ydpi, disp.refresh);
  1206.             }
  1207.  
  1208.             if (disp.list && disp.connected) {
  1209.                 result.appendFormat(
  1210.                         "  numHwLayers=%u, flags=%08x\n",
  1211.                         disp.list->numHwLayers, disp.list->flags);
  1212.  
  1213.                 result.append(
  1214.                         "    type    |  handle  |   hints  |   flags  | tr | blend |  format  |       source crop         |           frame           name \n"
  1215.                         "------------+----------+----------+----------+----+-------+----------+---------------------------+--------------------------------\n");
  1216.                 //      " __________ | ________ | ________ | ________ | __ | _____ | ________ | [_____,_____,_____,_____] | [_____,_____,_____,_____]
  1217.                 for (size_t i=0 ; i<disp.list->numHwLayers ; i++) {
  1218.                     const hwc_layer_1_t&l = disp.list->hwLayers[i];
  1219.                     int32_t format = -1;
  1220.                     String8 name("unknown");
  1221.  
  1222.                     if (i < visibleLayersSortedByZ.size()) {
  1223.                         const sp<LayerBase>& layer(visibleLayersSortedByZ[i]);
  1224.                         if (layer->getLayer() != NULL) {
  1225.                             const sp<GraphicBuffer>& buffer(
  1226.                                 layer->getLayer()->getActiveBuffer());
  1227.                             if (buffer != NULL) {
  1228.                                 format = buffer->getPixelFormat();
  1229.                             }
  1230.                         }
  1231.                         name = layer->getName();
  1232.                     }
  1233.  
  1234.                     int type = l.compositionType;
  1235.                     if (type == HWC_FRAMEBUFFER_TARGET) {
  1236.                         name = "HWC_FRAMEBUFFER_TARGET";
  1237.                         format = disp.format;
  1238.                     }
  1239.  
  1240.                     static char const* compositionTypeName[] = {
  1241.                             "GLES",
  1242.                             "HWC",
  1243.                             "BACKGROUND",
  1244.                             "FB TARGET",
  1245.                             "FB_BLIT",
  1246.                             "UNKNOWN"};
  1247.                     if (type >= NELEM(compositionTypeName))
  1248.                         type = NELEM(compositionTypeName) - 1;
  1249.  
  1250.                     result.appendFormat(
  1251.                             " %10s | %08x | %08x | %08x | %02x | %05x | %08x | [%5d,%5d,%5d,%5d] | [%5d,%5d,%5d,%5d] %s\n",
  1252.                                     compositionTypeName[type],
  1253.                                     intptr_t(l.handle), l.hints, l.flags, l.transform, l.blending, format,
  1254.                                     l.sourceCrop.left, l.sourceCrop.top, l.sourceCrop.right, l.sourceCrop.bottom,
  1255.                                     l.displayFrame.left, l.displayFrame.top, l.displayFrame.right, l.displayFrame.bottom,
  1256.                                     name.string());
  1257.                 }
  1258.             }
  1259.         }
  1260.     }
  1261.     if (mHwc) {
  1262.         hwcDump(mHwc, buffer, SIZE);
  1263.         result.append(buffer);
  1264.     }
  1265. }
  1266.  
  1267. // ---------------------------------------------------------------------------
  1268.  
  1269. HWComposer::VSyncThread::VSyncThread(HWComposer& hwc)
  1270.     : mHwc(hwc), mEnabled(false),
  1271.       mNextFakeVSync(0),
  1272.       mRefreshPeriod(hwc.getRefreshPeriod(HWC_DISPLAY_PRIMARY))
  1273. {
  1274. }
  1275.  
  1276. void HWComposer::VSyncThread::setEnabled(bool enabled) {
  1277.     Mutex::Autolock _l(mLock);
  1278.     if (mEnabled != enabled) {
  1279.         mEnabled = enabled;
  1280.         mCondition.signal();
  1281.     }
  1282. }
  1283.  
  1284. void HWComposer::VSyncThread::onFirstRef() {
  1285.     run("VSyncThread", PRIORITY_URGENT_DISPLAY + PRIORITY_MORE_FAVORABLE);
  1286. }
  1287.  
  1288. bool HWComposer::VSyncThread::threadLoop() {
  1289.     { // scope for lock
  1290.         Mutex::Autolock _l(mLock);
  1291.         while (!mEnabled) {
  1292.             mCondition.wait(mLock);
  1293.         }
  1294.     }
  1295.  
  1296.     const nsecs_t period = mRefreshPeriod;
  1297.     const nsecs_t now = systemTime(CLOCK_MONOTONIC);
  1298.     nsecs_t next_vsync = mNextFakeVSync;
  1299.     nsecs_t sleep = next_vsync - now;
  1300.     if (sleep < 0) {
  1301.         // we missed, find where the next vsync should be
  1302.         sleep = (period - ((now - next_vsync) % period));
  1303.         next_vsync = now + sleep;
  1304.     }
  1305.     mNextFakeVSync = next_vsync + period;
  1306.  
  1307.     struct timespec spec;
  1308.     spec.tv_sec  = next_vsync / 1000000000;
  1309.     spec.tv_nsec = next_vsync % 1000000000;
  1310.  
  1311.     int err;
  1312.     do {
  1313.         err = clock_nanosleep(CLOCK_MONOTONIC, TIMER_ABSTIME, &spec, NULL);
  1314.     } while (err<0 && errno == EINTR);
  1315.  
  1316.     if (err == 0 && mEnabled) {
  1317.         mHwc.mEventHandler.onVSyncReceived(0, next_vsync);
  1318.     }
  1319.  
  1320.     return true;
  1321. }
  1322.  
  1323. // ---------------------------------------------------------------------------
  1324. }; // namespace android
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement