diff --git a/graphics/composer/2.1/default/Android.bp b/graphics/composer/2.1/default/Android.bp index d5da9439b5..0367fcd0eb 100644 --- a/graphics/composer/2.1/default/Android.bp +++ b/graphics/composer/2.1/default/Android.bp @@ -37,6 +37,7 @@ cc_library_shared { "liblog", "libsync", "libutils", + "libhwc2on1adapter" ], } diff --git a/graphics/composer/2.1/default/Hwc.cpp b/graphics/composer/2.1/default/Hwc.cpp index cf82967e4d..e6ff9e00c3 100644 --- a/graphics/composer/2.1/default/Hwc.cpp +++ b/graphics/composer/2.1/default/Hwc.cpp @@ -22,6 +22,8 @@ #include "ComposerClient.h" #include "Hwc.h" +#include "hardware/hwcomposer.h" +#include "hwc2on1adapter/HWC2On1Adapter.h" namespace android { namespace hardware { @@ -30,13 +32,36 @@ namespace composer { namespace V2_1 { namespace implementation { + HwcHal::HwcHal(const hw_module_t* module) - : mDevice(nullptr), mDispatch() + : mDevice(nullptr), mDispatch(), mAdapter() { - int status = hwc2_open(module, &mDevice); - if (status) { - LOG_ALWAYS_FATAL("failed to open hwcomposer2 device: %s", - strerror(-status)); + // Determine what kind of module is available (HWC2 vs HWC1.X). + hw_device_t* device = nullptr; + int error = module->methods->open(module, HWC_HARDWARE_COMPOSER, &device); + if (error != 0) { + ALOGE("Failed to open HWC device (%s), aborting", strerror(-error)); + abort(); + } + uint32_t majorVersion = (device->version >> 24) & 0xF; + + // If we don't have a HWC2, we need to wrap whatever we have in an adapter. + if (majorVersion != 2) { + uint32_t minorVersion = device->version & HARDWARE_API_VERSION_2_MAJ_MIN_MASK; + minorVersion = (minorVersion >> 16) & 0xF; + ALOGI("Found HWC implementation v%d.%d", majorVersion, minorVersion); + if (minorVersion < 1) { + ALOGE("Cannot adapt to HWC version %d.%d. Minimum supported is 1.1", + majorVersion, minorVersion); + abort(); + } + mAdapter = std::make_unique( + reinterpret_cast(device)); + + // Place the adapter in front of the device module. + mDevice = mAdapter.get(); + } else { + mDevice = reinterpret_cast(device); } initCapabilities(); diff --git a/graphics/composer/2.1/default/Hwc.h b/graphics/composer/2.1/default/Hwc.h index ca08cf088a..b45389ad82 100644 --- a/graphics/composer/2.1/default/Hwc.h +++ b/graphics/composer/2.1/default/Hwc.h @@ -18,14 +18,22 @@ #define ANDROID_HARDWARE_GRAPHICS_COMPOSER_V2_1_HWC_H #include +#include #include #include #include +#define HWC2_INCLUDE_STRINGIFICATION +#define HWC2_USE_CPP11 #include - +#undef HWC2_INCLUDE_STRINGIFICATION +#undef HWC2_USE_CPP11 #include "ComposerBase.h" +namespace android { + class HWC2On1Adapter; +} + namespace android { namespace hardware { namespace graphics { @@ -204,6 +212,10 @@ private: std::mutex mClientMutex; wp mClient; + + // If the HWC implementation version is < 2.0, use an adapter to interface + // between HWC 2.0 <-> HWC 1.X. + std::unique_ptr mAdapter; }; extern "C" IComposer* HIDL_FETCH_IComposer(const char* name);