blob: 5958f57d7cf1db7e3c55818dafdab94fb9590770 [file] [log] [blame]
Sean Paulcd36a9e2015-01-22 18:01:18 -05001/*
2 * Copyright (C) 2015 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
Sean Paul74405612015-02-05 09:04:22 -080017#include <stdlib.h>
Sean Paulcd36a9e2015-01-22 18:01:18 -050018
19#include <cutils/log.h>
20
21#include <drm/drm_fourcc.h>
22
23#include <gralloc_drm.h>
24#include <gralloc_drm_priv.h>
25#include <gralloc_drm_handle.h>
26
27#include "drm_hwcomposer.h"
28
29struct hwc_import_context {
Sean Paulef8f1f92015-04-29 16:05:23 -040030 struct drm_module_t *gralloc_module;
Sean Paulcd36a9e2015-01-22 18:01:18 -050031};
32
Sean Paulef8f1f92015-04-29 16:05:23 -040033int hwc_import_init(struct hwc_import_context **ctx) {
34 struct hwc_import_context *import_ctx;
Sean Paulcd36a9e2015-01-22 18:01:18 -050035
Sean Paulef8f1f92015-04-29 16:05:23 -040036 import_ctx = (struct hwc_import_context *)calloc(1, sizeof(*import_ctx));
37 if (!ctx) {
38 ALOGE("Failed to allocate gralloc import context");
39 return -ENOMEM;
40 }
Sean Paulcd36a9e2015-01-22 18:01:18 -050041
Sean Paulef8f1f92015-04-29 16:05:23 -040042 int ret = hw_get_module(GRALLOC_HARDWARE_MODULE_ID,
43 (const hw_module_t **)&import_ctx->gralloc_module);
44 if (ret) {
45 ALOGE("Failed to open gralloc module");
46 free(import_ctx);
47 return ret;
48 }
Sean Paulcd36a9e2015-01-22 18:01:18 -050049
Sean Paulef8f1f92015-04-29 16:05:23 -040050 *ctx = import_ctx;
Sean Paulcd36a9e2015-01-22 18:01:18 -050051
Sean Paulef8f1f92015-04-29 16:05:23 -040052 return 0;
Sean Paulcd36a9e2015-01-22 18:01:18 -050053}
54
Sean Paulef8f1f92015-04-29 16:05:23 -040055int hwc_import_destroy(struct hwc_import_context *ctx) {
56 free(ctx);
57 return 0;
Sean Paulcd36a9e2015-01-22 18:01:18 -050058}
59
Sean Paulef8f1f92015-04-29 16:05:23 -040060static uint32_t hwc_convert_hal_format_to_drm_format(uint32_t hal_format) {
61 switch (hal_format) {
62 case HAL_PIXEL_FORMAT_RGB_888:
63 return DRM_FORMAT_BGR888;
64 case HAL_PIXEL_FORMAT_BGRA_8888:
65 return DRM_FORMAT_ARGB8888;
66 case HAL_PIXEL_FORMAT_RGBX_8888:
67 return DRM_FORMAT_XBGR8888;
68 case HAL_PIXEL_FORMAT_RGBA_8888:
69 return DRM_FORMAT_ABGR8888;
70 case HAL_PIXEL_FORMAT_RGB_565:
71 return DRM_FORMAT_BGR565;
72 case HAL_PIXEL_FORMAT_YV12:
73 return DRM_FORMAT_YVU420;
74 default:
75 ALOGE("Cannot convert hal format to drm format %u", hal_format);
76 return -EINVAL;
77 }
Sean Paulcd36a9e2015-01-22 18:01:18 -050078}
79
Lauri Peltonen77d6d7a2015-02-23 20:44:16 +020080int hwc_import_bo_create(int fd, hwc_import_context *ctx,
Sean Paulef8f1f92015-04-29 16:05:23 -040081 buffer_handle_t handle, struct hwc_drm_bo *bo) {
82 gralloc_drm_handle_t *gr_handle = gralloc_drm_handle(handle);
83 if (!gr_handle)
84 return -EINVAL;
Sean Paulaea15c22015-02-09 02:24:11 -050085
Sean Paulef8f1f92015-04-29 16:05:23 -040086 struct gralloc_drm_bo_t *gralloc_bo = gr_handle->data;
87 if (!gralloc_bo) {
88 ALOGE("Could not get drm bo from handle");
89 return -EINVAL;
90 }
Sean Paulcd36a9e2015-01-22 18:01:18 -050091
Sean Paulef8f1f92015-04-29 16:05:23 -040092 uint32_t gem_handle;
93 int ret = drmPrimeFDToHandle(fd, gr_handle->prime_fd, &gem_handle);
94 if (ret) {
95 ALOGE("failed to import prime fd %d ret=%d", gr_handle->prime_fd, ret);
96 return ret;
97 }
Sean Paulcd36a9e2015-01-22 18:01:18 -050098
Sean Paulef8f1f92015-04-29 16:05:23 -040099 bo->width = gr_handle->width;
100 bo->height = gr_handle->height;
101 bo->format = hwc_convert_hal_format_to_drm_format(gr_handle->format);
102 bo->pitches[0] = gr_handle->stride;
103 bo->gem_handles[0] = gem_handle;
104 bo->offsets[0] = 0;
Sean Paulaea15c22015-02-09 02:24:11 -0500105
Sean Paulef8f1f92015-04-29 16:05:23 -0400106 ret = drmModeAddFB2(fd, bo->width, bo->height, bo->format, bo->gem_handles,
107 bo->pitches, bo->offsets, &bo->fb_id, 0);
108 if (ret) {
109 ALOGE("could not create drm fb %d", ret);
110 return ret;
111 }
Sean Paulcd36a9e2015-01-22 18:01:18 -0500112
Sean Paulef8f1f92015-04-29 16:05:23 -0400113 return ret;
Lauri Peltonen77d6d7a2015-02-23 20:44:16 +0200114}
115
Sean Paulef8f1f92015-04-29 16:05:23 -0400116bool hwc_import_bo_release(int fd, hwc_import_context */* ctx */,
117 struct hwc_drm_bo *bo) {
118 if (bo->fb_id)
119 if (drmModeRmFB(fd, bo->fb_id))
120 ALOGE("Failed to rm fb");
Lauri Peltonen77d6d7a2015-02-23 20:44:16 +0200121
Sean Paulef8f1f92015-04-29 16:05:23 -0400122 /* hwc may close the gem handles now. */
123 return true;
Sean Paulcd36a9e2015-01-22 18:01:18 -0500124}