OSDN Git Service

HACK: add rotation defines
[android-x86/external-drm_hwcomposer.git] / drmgenericimporter.cpp
1 /*
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 #define LOG_TAG "hwc-drm-generic-importer"
18
19 #include "importer.h"
20 #include "drmresources.h"
21 #include "drmgenericimporter.h"
22
23 #include <drm/drm_fourcc.h>
24 #include <xf86drm.h>
25 #include <xf86drmMode.h>
26
27 #include <cutils/log.h>
28 #include <gralloc_drm_handle.h>
29 #include <hardware/gralloc.h>
30
31 namespace android {
32
33 #ifdef USE_DRM_GENERIC_IMPORTER
34 // static
35 Importer *Importer::CreateInstance(DrmResources *drm) {
36   DrmGenericImporter *importer = new DrmGenericImporter(drm);
37   if (!importer)
38     return NULL;
39
40   int ret = importer->Init();
41   if (ret) {
42     ALOGE("Failed to initialize the nv importer %d", ret);
43     delete importer;
44     return NULL;
45   }
46   return importer;
47 }
48 #endif
49
50 DrmGenericImporter::DrmGenericImporter(DrmResources *drm) : drm_(drm) {
51 }
52
53 DrmGenericImporter::~DrmGenericImporter() {
54 }
55
56 int DrmGenericImporter::Init() {
57   int ret = hw_get_module(GRALLOC_HARDWARE_MODULE_ID,
58                           (const hw_module_t **)&gralloc_);
59   if (ret) {
60     ALOGE("Failed to open gralloc module");
61     return ret;
62   }
63   return 0;
64 }
65
66 uint32_t DrmGenericImporter::ConvertHalFormatToDrm(uint32_t hal_format) {
67   switch (hal_format) {
68     case HAL_PIXEL_FORMAT_RGB_888:
69       return DRM_FORMAT_BGR888;
70     case HAL_PIXEL_FORMAT_BGRA_8888:
71       return DRM_FORMAT_ARGB8888;
72     case HAL_PIXEL_FORMAT_RGBX_8888:
73       return DRM_FORMAT_XBGR8888;
74     case HAL_PIXEL_FORMAT_RGBA_8888:
75       return DRM_FORMAT_ABGR8888;
76     case HAL_PIXEL_FORMAT_RGB_565:
77       return DRM_FORMAT_BGR565;
78     case HAL_PIXEL_FORMAT_YV12:
79       return DRM_FORMAT_YVU420;
80     default:
81       ALOGE("Cannot convert hal format to drm format %u", hal_format);
82       return -EINVAL;
83   }
84 }
85
86 int DrmGenericImporter::ImportBuffer(buffer_handle_t handle, hwc_drm_bo_t *bo) {
87   gralloc_drm_handle_t *gr_handle = gralloc_drm_handle(handle);
88   if (!gr_handle)
89     return -EINVAL;
90
91   uint32_t gem_handle;
92   int ret = drmPrimeFDToHandle(drm_->fd(), gr_handle->prime_fd, &gem_handle);
93   if (ret) {
94     ALOGE("failed to import prime fd %d ret=%d", gr_handle->prime_fd, ret);
95     return ret;
96   }
97
98   memset(bo, 0, sizeof(hwc_drm_bo_t));
99   bo->width = gr_handle->width;
100   bo->height = gr_handle->height;
101   bo->format = ConvertHalFormatToDrm(gr_handle->format);
102   bo->pitches[0] = gr_handle->stride;
103   bo->gem_handles[0] = gem_handle;
104   bo->offsets[0] = 0;
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 int DrmGenericImporter::ReleaseBuffer(hwc_drm_bo_t *bo) {
117   if (bo->fb_id)
118     if (drmModeRmFB(drm_->fd(), bo->fb_id))
119       ALOGE("Failed to rm fb");
120
121   struct drm_gem_close gem_close;
122   memset(&gem_close, 0, sizeof(gem_close));
123   int num_gem_handles = sizeof(bo->gem_handles) / sizeof(bo->gem_handles[0]);
124   for (int i = 0; i < num_gem_handles; i++) {
125     if (!bo->gem_handles[i])
126       continue;
127
128     gem_close.handle = bo->gem_handles[i];
129     int ret = drmIoctl(drm_->fd(), DRM_IOCTL_GEM_CLOSE, &gem_close);
130     if (ret)
131       ALOGE("Failed to close gem handle %d %d", i, ret);
132     else
133       bo->gem_handles[i] = 0;
134   }
135   return 0;
136 }
137 }