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 | |
Sean Paul | f72cccd | 2018-08-27 13:59:08 -0400 | [diff] [blame] | 19 | #include "platformminigbm.h" |
Alistair Strachan | 71edaca | 2018-05-02 17:01:49 -0700 | [diff] [blame] | 20 | #include "drmresources.h" |
| 21 | #include "platform.h" |
Alistair Strachan | 71edaca | 2018-05-02 17:01:49 -0700 | [diff] [blame] | 22 | |
| 23 | #include <drm/drm_fourcc.h> |
| 24 | #include <xf86drm.h> |
| 25 | #include <xf86drmMode.h> |
| 26 | |
Alistair Strachan | 71edaca | 2018-05-02 17:01:49 -0700 | [diff] [blame] | 27 | #include <hardware/gralloc.h> |
Sean Paul | f72cccd | 2018-08-27 13:59:08 -0400 | [diff] [blame] | 28 | #include <log/log.h> |
Alistair Strachan | 71edaca | 2018-05-02 17:01:49 -0700 | [diff] [blame] | 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 | |
Sean Paul | f72cccd | 2018-08-27 13:59:08 -0400 | [diff] [blame] | 48 | DrmMinigbmImporter::DrmMinigbmImporter(DrmResources *drm) |
| 49 | : DrmGenericImporter(drm), drm_(drm) { |
Alistair Strachan | 71edaca | 2018-05-02 17:01:49 -0700 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | DrmMinigbmImporter::~DrmMinigbmImporter() { |
| 53 | } |
| 54 | |
| 55 | int DrmMinigbmImporter::Init() { |
| 56 | int ret = hw_get_module(GRALLOC_HARDWARE_MODULE_ID, |
| 57 | (const hw_module_t **)&gralloc_); |
| 58 | if (ret) { |
| 59 | ALOGE("Failed to open gralloc module %d", ret); |
| 60 | return ret; |
| 61 | } |
| 62 | |
| 63 | if (strcasecmp(gralloc_->common.author, "Chrome OS")) |
| 64 | ALOGW("Using non-minigbm gralloc module: %s/%s\n", gralloc_->common.name, |
| 65 | gralloc_->common.author); |
| 66 | |
| 67 | return 0; |
| 68 | } |
| 69 | |
Alistair Strachan | 71edaca | 2018-05-02 17:01:49 -0700 | [diff] [blame] | 70 | int DrmMinigbmImporter::ImportBuffer(buffer_handle_t handle, hwc_drm_bo_t *bo) { |
| 71 | cros_gralloc_handle *gr_handle = (cros_gralloc_handle *)handle; |
| 72 | if (!gr_handle) |
| 73 | return -EINVAL; |
| 74 | |
| 75 | uint32_t gem_handle; |
| 76 | int ret = drmPrimeFDToHandle(drm_->fd(), gr_handle->fds[0], &gem_handle); |
| 77 | if (ret) { |
| 78 | ALOGE("failed to import prime fd %d ret=%d", gr_handle->fds[0], ret); |
| 79 | return ret; |
| 80 | } |
| 81 | |
| 82 | memset(bo, 0, sizeof(hwc_drm_bo_t)); |
| 83 | bo->width = gr_handle->width; |
| 84 | bo->height = gr_handle->height; |
John Stultz | 616fb22 | 2018-08-24 16:08:57 -0700 | [diff] [blame^] | 85 | bo->hal_format = gr_handle->droid_format; |
Alistair Strachan | 71edaca | 2018-05-02 17:01:49 -0700 | [diff] [blame] | 86 | bo->format = gr_handle->format; |
| 87 | bo->usage = gr_handle->usage; |
| 88 | bo->pitches[0] = gr_handle->strides[0]; |
| 89 | bo->offsets[0] = gr_handle->offsets[0]; |
| 90 | bo->gem_handles[0] = gem_handle; |
| 91 | |
| 92 | ret = drmModeAddFB2(drm_->fd(), bo->width, bo->height, bo->format, |
| 93 | bo->gem_handles, bo->pitches, bo->offsets, &bo->fb_id, 0); |
| 94 | if (ret) { |
| 95 | ALOGE("could not create drm fb %d", ret); |
| 96 | return ret; |
| 97 | } |
| 98 | |
| 99 | return ret; |
| 100 | } |
| 101 | |
| 102 | std::unique_ptr<Planner> Planner::CreateInstance(DrmResources *) { |
| 103 | std::unique_ptr<Planner> planner(new Planner); |
| 104 | planner->AddStage<PlanStageGreedy>(); |
| 105 | return planner; |
| 106 | } |
| 107 | |
Sean Paul | f72cccd | 2018-08-27 13:59:08 -0400 | [diff] [blame] | 108 | } // namespace android |