OSDN Git Service

Android-x86: Implement some perform functionality from drm_gralloc (v2)
[android-x86/external-minigbm.git] / cros_gralloc / gralloc0 / gralloc0.cc
index b8ef8f7..900e376 100644 (file)
 #include <cassert>
 #include <hardware/gralloc.h>
 #include <memory.h>
+#include <xf86drm.h>
+#include "drm_framebuffer.h"
+#include "gralloc_drm.h"
 
 struct gralloc0_module {
        gralloc_module_t base;
        std::unique_ptr<alloc_device_t> alloc;
        std::unique_ptr<cros_gralloc_driver> driver;
+       struct drm_framebuffer *fb;
        bool initialized;
        std::mutex initialization_mutex;
 };
@@ -74,7 +78,7 @@ static uint64_t gralloc0_convert_usage(int usage)
                /* HWC wants to use display hardware, but can defer to OpenGL. */
                use_flags |= BO_USE_SCANOUT | BO_USE_TEXTURE;
        if (usage & GRALLOC_USAGE_HW_FB)
-               use_flags |= BO_USE_COMPOSER_TARGET;
+               use_flags |= BO_USE_FRAMEBUFFER;
        if (usage & GRALLOC_USAGE_EXTERNAL_DISP)
                /*
                 * This flag potentially covers external display for the normal drivers (i915,
@@ -138,7 +142,7 @@ static int gralloc0_alloc(alloc_device_t *dev, int w, int h, int format, int usa
 
        supported = mod->driver->is_supported(&descriptor);
        if (!supported && (usage & GRALLOC_USAGE_HW_COMPOSER)) {
-               descriptor.use_flags &= ~(BO_USE_SCANOUT | BO_USE_COMPOSER_TARGET);
+               descriptor.use_flags &= ~BO_USE_SCANOUT;
                supported = mod->driver->is_supported(&descriptor);
        }
        if (!supported && (usage & GRALLOC_USAGE_HW_VIDEO_ENCODER) &&
@@ -180,7 +184,8 @@ static int gralloc0_close(struct hw_device_t *dev)
        return 0;
 }
 
-static int gralloc0_init(struct gralloc0_module *mod, bool initialize_alloc)
+static int gralloc0_init(struct gralloc0_module *mod, bool initialize_alloc,
+                         bool framebuffer = false)
 {
        std::lock_guard<std::mutex> lock(mod->initialization_mutex);
 
@@ -188,7 +193,7 @@ static int gralloc0_init(struct gralloc0_module *mod, bool initialize_alloc)
                return 0;
 
        mod->driver = std::make_unique<cros_gralloc_driver>();
-       if (mod->driver->init()) {
+       if (framebuffer ? mod->driver->init_master() : mod->driver->init()) {
                drv_log("Failed to initialize driver.\n");
                return -ENODEV;
        }
@@ -203,15 +208,58 @@ static int gralloc0_init(struct gralloc0_module *mod, bool initialize_alloc)
                mod->alloc->common.close = gralloc0_close;
        }
 
+       if (framebuffer) {
+               int ret = drm_framebuffer_init(mod->driver->get_fd(), &mod->fb);
+               if (ret)
+                       return ret;
+       }
+
        mod->initialized = true;
        return 0;
 }
 
+static int gralloc0_open_fb0(struct gralloc0_module *mod, struct hw_device_t **dev)
+{
+       int ret;
+
+       if (!mod->initialized) {
+               ret = gralloc0_init(mod, true, true);
+               if (ret)
+                       return ret;
+       }
+
+       if (!mod->fb) {
+               /*
+                * On Pie and above the FB HAL is opened before the Gralloc HAL.
+                * This has the advantage that we can open the DRM card node in this case,
+                * and open the render node in all other cases.
+                *
+                * On earlier Android versions this is not the case, so we need to make
+                * sure the FB HAL was actually initialized.
+                *
+                * TODO: Currently it does not attempt to set master on the opened render
+                * node. That means it will only work with DRM authentication disabled.
+                */
+               std::lock_guard<std::mutex> lock(mod->initialization_mutex);
+               drv_log("FB HAL opened after Gralloc HAL, we might not be DRM master!\n");
+
+               ret = drm_framebuffer_init(mod->driver->get_fd(), &mod->fb);
+               if (ret)
+                       return ret;
+       }
+
+       *dev = (struct hw_device_t *) mod->fb;
+       return 0;
+}
+
 static int gralloc0_open(const struct hw_module_t *mod, const char *name, struct hw_device_t **dev)
 {
        auto const_module = reinterpret_cast<const struct gralloc0_module *>(mod);
        auto module = const_cast<struct gralloc0_module *>(const_module);
 
+       if (!strcmp(name, GRALLOC_HARDWARE_FB0))
+               return gralloc0_open_fb0(module, dev);
+
        if (module->initialized) {
                *dev = &module->alloc->common;
                return 0;
@@ -238,7 +286,10 @@ static int gralloc0_register_buffer(struct gralloc_module_t const *module, buffe
                if (gralloc0_init(mod, false))
                        return -ENODEV;
 
-       return mod->driver->retain(handle);
+       int ret = mod->driver->retain(handle);
+       if (ret == 0 && mod->fb)
+               drm_framebuffer_import(mod->fb, handle);
+       return ret;
 }
 
 static int gralloc0_unregister_buffer(struct gralloc_module_t const *module, buffer_handle_t handle)
@@ -281,6 +332,9 @@ static int gralloc0_perform(struct gralloc_module_t const *module, int op, ...)
        auto mod = (struct gralloc0_module const *)module;
 
        switch (op) {
+       case GRALLOC_MODULE_PERFORM_GET_DRM_FD:
+       case GRALLOC_MODULE_PERFORM_ENTER_VT:
+       case GRALLOC_MODULE_PERFORM_LEAVE_VT:
        case GRALLOC_DRM_GET_STRIDE:
        case GRALLOC_DRM_GET_FORMAT:
        case GRALLOC_DRM_GET_DIMENSIONS:
@@ -292,8 +346,28 @@ static int gralloc0_perform(struct gralloc_module_t const *module, int op, ...)
        }
 
        va_start(args, op);
-
        ret = 0;
+
+       switch (op) {
+       case GRALLOC_MODULE_PERFORM_GET_DRM_FD: {
+               int *fd = va_arg(args, int*);
+               *fd = mod->driver->get_fd();
+               break;
+       }
+       case GRALLOC_MODULE_PERFORM_ENTER_VT:
+               ret = drmSetMaster(mod->driver->get_fd());
+               break;
+       case GRALLOC_MODULE_PERFORM_LEAVE_VT:
+               ret = drmDropMaster(mod->driver->get_fd());
+               break;
+       default:
+               goto other;
+       }
+
+       va_end(args);
+       return ret;
+
+other:
        handle = va_arg(args, buffer_handle_t);
        auto hnd = cros_gralloc_convert_handle(handle);
        if (!hnd) {