blob: 065170cb20a2ea5a0d8aa3c9873bdec6a6ceb7f5 [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
17
18#include <cutils/log.h>
19
20#include <drm/drm_fourcc.h>
21
22#include <gralloc_drm.h>
23#include <gralloc_drm_priv.h>
24#include <gralloc_drm_handle.h>
25
26#include "drm_hwcomposer.h"
27
28struct hwc_import_context {
29 struct drm_module_t *gralloc_module;
30};
31
32int hwc_import_init(struct hwc_import_context **ctx)
33{
34 int ret;
35 struct hwc_import_context *import_ctx;
36
37 import_ctx = (struct hwc_import_context *)calloc(1,
38 sizeof(*import_ctx));
39 if (!ctx) {
40 ALOGE("Failed to allocate gralloc import context");
41 return -ENOMEM;
42 }
43
44 ret = hw_get_module(GRALLOC_HARDWARE_MODULE_ID,
45 (const hw_module_t **)&import_ctx->gralloc_module);
46 if (ret) {
47 ALOGE("Failed to open gralloc module");
48 goto err;
49 }
50
51 *ctx = import_ctx;
52
53 return 0;
54
55err:
56 free(import_ctx);
57 return ret;
58}
59
60int hwc_import_destroy(struct hwc_import_context *ctx)
61{
62 free(ctx);
63 return 0;
64}
65
66static uint32_t hwc_convert_hal_format_to_drm_format(uint32_t hal_format)
67{
68 switch(hal_format) {
69 case HAL_PIXEL_FORMAT_RGB_888:
70 return DRM_FORMAT_BGR888;
71 case HAL_PIXEL_FORMAT_BGRA_8888:
72 return DRM_FORMAT_ARGB8888;
73 case HAL_PIXEL_FORMAT_RGBX_8888:
74 return DRM_FORMAT_XBGR8888;
75 case HAL_PIXEL_FORMAT_RGBA_8888:
76 return DRM_FORMAT_ABGR8888;
77 case HAL_PIXEL_FORMAT_RGB_565:
78 return DRM_FORMAT_BGR565;
79 case HAL_PIXEL_FORMAT_YV12:
80 return DRM_FORMAT_YVU420;
81 default:
82 ALOGE("Cannot convert hal format to drm format %u", hal_format);
83 return -EINVAL;
84 }
85}
86
87int hwc_create_bo_from_import(int /* fd */, hwc_import_context *ctx,
88 buffer_handle_t handle, struct hwc_drm_bo *bo)
89{
90 gralloc_drm_t *drm = ctx->gralloc_module->drm;
91 gralloc_drm_handle_t *gr_handle;
92 struct gralloc_drm_bo_t *gralloc_bo;
93 gr_handle = (gralloc_drm_handle_t *)handle;
94
95 gralloc_bo = gr_handle->data;
96 if (!gralloc_bo) {
97 ALOGE("Could not get drm bo from handle");
98 return -EINVAL;
99 }
100
101 bo->importer_fd = drm->fd;
102 bo->width = gr_handle->width;
103 bo->height = gr_handle->height;
104 bo->format = hwc_convert_hal_format_to_drm_format(gr_handle->format);
105 bo->pitches[0] = gr_handle->stride;
106 bo->gem_handles[0] = gralloc_bo->fb_handle;
107 bo->offsets[0] = 0;
108
109 return 0;
110}