Alistair Strachan | 71edaca | 2018-05-02 17:01:49 -0700 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (C) 2018 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 LOG_TAG "hwc-platform-drm-minigbm" |
| 18 | |
| 19 | #include "drmresources.h" |
| 20 | #include "platform.h" |
| 21 | #include "platformminigbm.h" |
| 22 | |
| 23 | #include <drm/drm_fourcc.h> |
| 24 | #include <xf86drm.h> |
| 25 | #include <xf86drmMode.h> |
| 26 | |
| 27 | #include <log/log.h> |
| 28 | #include <hardware/gralloc.h> |
| 29 | |
| 30 | #include "cros_gralloc_handle.h" |
| 31 | |
| 32 | namespace android { |
| 33 | |
| 34 | Importer *Importer::CreateInstance(DrmResources *drm) { |
| 35 | DrmMinigbmImporter *importer = new DrmMinigbmImporter(drm); |
| 36 | if (!importer) |
| 37 | return NULL; |
| 38 | |
| 39 | int ret = importer->Init(); |
| 40 | if (ret) { |
| 41 | ALOGE("Failed to initialize the minigbm importer %d", ret); |
| 42 | delete importer; |
| 43 | return NULL; |
| 44 | } |
| 45 | return importer; |
| 46 | } |
| 47 | |
| 48 | DrmMinigbmImporter::DrmMinigbmImporter(DrmResources *drm) : DrmGenericImporter(drm), drm_(drm) { |
| 49 | } |
| 50 | |
| 51 | DrmMinigbmImporter::~DrmMinigbmImporter() { |
| 52 | } |
| 53 | |
| 54 | int DrmMinigbmImporter::Init() { |
| 55 | int ret = hw_get_module(GRALLOC_HARDWARE_MODULE_ID, |
| 56 | (const hw_module_t **)&gralloc_); |
| 57 | if (ret) { |
| 58 | ALOGE("Failed to open gralloc module %d", ret); |
| 59 | return ret; |
| 60 | } |
| 61 | |
| 62 | if (strcasecmp(gralloc_->common.author, "Chrome OS")) |
| 63 | ALOGW("Using non-minigbm gralloc module: %s/%s\n", gralloc_->common.name, |
| 64 | gralloc_->common.author); |
| 65 | |
| 66 | return 0; |
| 67 | } |
| 68 | |
| 69 | EGLImageKHR DrmMinigbmImporter::ImportImage(EGLDisplay egl_display, buffer_handle_t handle) { |
| 70 | cros_gralloc_handle *gr_handle = (cros_gralloc_handle *)handle; |
| 71 | if (!gr_handle) |
| 72 | return NULL; |
| 73 | EGLint attr[] = { |
| 74 | EGL_WIDTH, (EGLint)gr_handle->width, |
| 75 | EGL_HEIGHT, (EGLint)gr_handle->height, |
| 76 | EGL_LINUX_DRM_FOURCC_EXT, (EGLint)gr_handle->format, |
| 77 | EGL_DMA_BUF_PLANE0_FD_EXT, gr_handle->fds[0], |
| 78 | EGL_DMA_BUF_PLANE0_PITCH_EXT, (EGLint)gr_handle->strides[0], |
| 79 | EGL_DMA_BUF_PLANE0_OFFSET_EXT, (EGLint)gr_handle->offsets[0], |
| 80 | EGL_NONE, |
| 81 | }; |
| 82 | return eglCreateImageKHR(egl_display, EGL_NO_CONTEXT, EGL_LINUX_DMA_BUF_EXT, NULL, attr); |
| 83 | } |
| 84 | |
| 85 | int DrmMinigbmImporter::ImportBuffer(buffer_handle_t handle, hwc_drm_bo_t *bo) { |
| 86 | cros_gralloc_handle *gr_handle = (cros_gralloc_handle *)handle; |
| 87 | if (!gr_handle) |
| 88 | return -EINVAL; |
| 89 | |
| 90 | uint32_t gem_handle; |
| 91 | int ret = drmPrimeFDToHandle(drm_->fd(), gr_handle->fds[0], &gem_handle); |
| 92 | if (ret) { |
| 93 | ALOGE("failed to import prime fd %d ret=%d", gr_handle->fds[0], ret); |
| 94 | return ret; |
| 95 | } |
| 96 | |
| 97 | memset(bo, 0, sizeof(hwc_drm_bo_t)); |
| 98 | bo->width = gr_handle->width; |
| 99 | bo->height = gr_handle->height; |
| 100 | bo->format = gr_handle->format; |
| 101 | bo->usage = gr_handle->usage; |
| 102 | bo->pitches[0] = gr_handle->strides[0]; |
| 103 | bo->offsets[0] = gr_handle->offsets[0]; |
| 104 | bo->gem_handles[0] = gem_handle; |
| 105 | |
| 106 | ret = drmModeAddFB2(drm_->fd(), bo->width, bo->height, bo->format, |
| 107 | bo->gem_handles, bo->pitches, bo->offsets, &bo->fb_id, 0); |
| 108 | if (ret) { |
| 109 | ALOGE("could not create drm fb %d", ret); |
| 110 | return ret; |
| 111 | } |
| 112 | |
| 113 | return ret; |
| 114 | } |
| 115 | |
| 116 | std::unique_ptr<Planner> Planner::CreateInstance(DrmResources *) { |
| 117 | std::unique_ptr<Planner> planner(new Planner); |
| 118 | planner->AddStage<PlanStageGreedy>(); |
| 119 | return planner; |
| 120 | } |
| 121 | |
| 122 | } |