Lauri Peltonen | ceaadea | 2015-02-02 14:45:13 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 The Android Open Source Project |
| 3 | * Copyright (C) 2015 NVIDIA Corporation. |
| 4 | * |
| 5 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | * you may not use this file except in compliance with the License. |
| 7 | * You may obtain a copy of the License at |
| 8 | * |
| 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | * |
| 11 | * Unless required by applicable law or agreed to in writing, software |
| 12 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | * See the License for the specific language governing permissions and |
| 15 | * limitations under the License. |
| 16 | */ |
| 17 | |
| 18 | |
| 19 | #include <cutils/log.h> |
| 20 | #include <hardware/gralloc.h> |
| 21 | |
David Riley | 65051b4 | 2015-03-26 21:50:11 -0700 | [diff] [blame^] | 22 | #include <stdlib.h> |
Lauri Peltonen | 77d6d7a | 2015-02-23 20:44:16 +0200 | [diff] [blame] | 23 | #include <xf86drm.h> |
| 24 | #include <xf86drmMode.h> |
Lauri Peltonen | ceaadea | 2015-02-02 14:45:13 +0200 | [diff] [blame] | 25 | #include "drm_hwcomposer.h" |
| 26 | |
Lauri Peltonen | 77d6d7a | 2015-02-23 20:44:16 +0200 | [diff] [blame] | 27 | #define ARRAY_SIZE(arr) (int)(sizeof(arr) / sizeof((arr)[0])) |
| 28 | |
Lauri Peltonen | ceaadea | 2015-02-02 14:45:13 +0200 | [diff] [blame] | 29 | struct hwc_import_context { |
| 30 | const gralloc_module_t *gralloc_module; |
| 31 | }; |
| 32 | |
| 33 | int hwc_import_init(struct hwc_import_context **ctx) |
| 34 | { |
| 35 | int ret; |
| 36 | struct hwc_import_context *import_ctx; |
| 37 | |
| 38 | import_ctx = (struct hwc_import_context *)calloc(1, |
| 39 | sizeof(*import_ctx)); |
| 40 | if (!ctx) { |
| 41 | ALOGE("Failed to allocate gralloc import context"); |
| 42 | return -ENOMEM; |
| 43 | } |
| 44 | |
| 45 | ret = hw_get_module(GRALLOC_HARDWARE_MODULE_ID, |
| 46 | (const hw_module_t **)&import_ctx->gralloc_module); |
| 47 | if (ret) { |
| 48 | ALOGE("Failed to open gralloc module"); |
| 49 | goto err; |
| 50 | } |
| 51 | |
| 52 | if (!strcasecmp(import_ctx->gralloc_module->common.author, "NVIDIA")) |
| 53 | ALOGW("Using non-NVIDIA gralloc module: %s\n", |
| 54 | import_ctx->gralloc_module->common.name); |
| 55 | |
| 56 | *ctx = import_ctx; |
| 57 | |
| 58 | return 0; |
| 59 | |
| 60 | err: |
| 61 | free(import_ctx); |
| 62 | return ret; |
| 63 | } |
| 64 | |
| 65 | int hwc_import_destroy(struct hwc_import_context *ctx) |
| 66 | { |
| 67 | free(ctx); |
| 68 | return 0; |
| 69 | } |
| 70 | |
Lauri Peltonen | 77d6d7a | 2015-02-23 20:44:16 +0200 | [diff] [blame] | 71 | struct importer_priv |
Lauri Peltonen | ceaadea | 2015-02-02 14:45:13 +0200 | [diff] [blame] | 72 | { |
Lauri Peltonen | 77d6d7a | 2015-02-23 20:44:16 +0200 | [diff] [blame] | 73 | int drm_fd; |
| 74 | struct hwc_drm_bo bo; |
| 75 | }; |
| 76 | |
| 77 | static void free_priv(void *p) |
| 78 | { |
| 79 | struct importer_priv *priv = (struct importer_priv *)p; |
| 80 | struct drm_gem_close gem_close; |
| 81 | int i, ret; |
| 82 | |
| 83 | if (priv->bo.fb_id) { |
| 84 | ret = drmModeRmFB(priv->drm_fd, priv->bo.fb_id); |
| 85 | if (ret) |
| 86 | ALOGE("Failed to rm fb %d", ret); |
| 87 | } |
| 88 | |
| 89 | memset(&gem_close, 0, sizeof(gem_close)); |
| 90 | for (i = 0; i < ARRAY_SIZE(priv->bo.gem_handles); i++) { |
| 91 | if (!priv->bo.gem_handles[i]) |
| 92 | continue; |
| 93 | gem_close.handle = priv->bo.gem_handles[i]; |
| 94 | ret = drmIoctl(priv->drm_fd, DRM_IOCTL_GEM_CLOSE, &gem_close); |
| 95 | if (ret) |
| 96 | ALOGE("Failed to close gem handle %d", ret); |
| 97 | } |
| 98 | |
| 99 | free(priv); |
| 100 | } |
| 101 | |
| 102 | static int |
| 103 | hwc_import_set_priv(hwc_import_context *ctx, buffer_handle_t handle, struct importer_priv *priv) |
| 104 | { |
| 105 | int ret; |
Lauri Peltonen | ceaadea | 2015-02-02 14:45:13 +0200 | [diff] [blame] | 106 | const gralloc_module_t *g = ctx->gralloc_module; |
| 107 | |
Lauri Peltonen | 77d6d7a | 2015-02-23 20:44:16 +0200 | [diff] [blame] | 108 | return g->perform(g, GRALLOC_MODULE_PERFORM_SET_IMPORTER_PRIVATE, handle, free_priv, priv); |
| 109 | } |
| 110 | |
| 111 | static struct importer_priv * |
| 112 | hwc_import_get_priv(hwc_import_context *ctx, buffer_handle_t handle) |
| 113 | { |
| 114 | int ret; |
| 115 | void *priv = NULL; |
| 116 | const gralloc_module_t *g = ctx->gralloc_module; |
| 117 | |
| 118 | ret = g->perform(g, GRALLOC_MODULE_PERFORM_GET_IMPORTER_PRIVATE, handle, free_priv, &priv); |
| 119 | return ret ? NULL : (struct importer_priv *)priv; |
| 120 | } |
| 121 | |
| 122 | int hwc_import_bo_create(int fd, hwc_import_context *ctx, |
| 123 | buffer_handle_t handle, struct hwc_drm_bo *bo) |
| 124 | { |
| 125 | int ret = 0; |
| 126 | const gralloc_module_t *g = ctx->gralloc_module; |
| 127 | |
| 128 | /* Get imported bo that is cached in gralloc buffer, or create a new one. */ |
| 129 | struct importer_priv *priv = hwc_import_get_priv(ctx, handle); |
| 130 | if (!priv) { |
| 131 | priv = (struct importer_priv *)calloc(1, sizeof(*priv)); |
| 132 | if (!priv) |
| 133 | return -ENOMEM; |
| 134 | priv->drm_fd = fd; |
| 135 | |
| 136 | ret = g->perform(g, GRALLOC_MODULE_PERFORM_DRM_IMPORT, fd, handle, &priv->bo); |
| 137 | if (ret) { |
| 138 | ALOGE("GRALLOC_MODULE_PERFORM_DRM_IMPORT failed %d", ret); |
| 139 | free_priv(priv); |
| 140 | return ret; |
| 141 | } |
| 142 | |
| 143 | ret = drmModeAddFB2(fd, priv->bo.width, priv->bo.height, |
| 144 | priv->bo.format, priv->bo.gem_handles, |
| 145 | priv->bo.pitches, priv->bo.offsets, |
| 146 | &priv->bo.fb_id, 0); |
| 147 | if (ret) { |
| 148 | ALOGE("Failed to add fb %d", ret); |
| 149 | free_priv(priv); |
| 150 | return ret; |
| 151 | } |
| 152 | |
| 153 | ret = hwc_import_set_priv(ctx, handle, priv); |
| 154 | if (ret) { |
| 155 | /* This will happen is persist.tegra.gpu_mapping_cache is 0/off, |
| 156 | * or if NV gralloc runs out of "priv slots" (currently 3 per buffer, |
| 157 | * only one of which should be used by drm_hwcomposer). */ |
| 158 | ALOGE("Failed to register free callback for imported buffer %d", ret); |
| 159 | free_priv(priv); |
| 160 | return ret; |
| 161 | } |
| 162 | } |
| 163 | *bo = priv->bo; |
| 164 | return ret; |
| 165 | } |
| 166 | |
| 167 | bool hwc_import_bo_release(int fd, hwc_import_context *ctx, |
| 168 | struct hwc_drm_bo *bo) |
| 169 | { |
| 170 | /* hwc may not close the gem handles, we own them */ |
| 171 | return false; |
Lauri Peltonen | ceaadea | 2015-02-02 14:45:13 +0200 | [diff] [blame] | 172 | } |