OSDN Git Service

drm_hwcomposer: drmeventlistener: Set nl_pid to 0
[android-x86/external-drm_hwcomposer.git] / platformhisi.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-platform-hisi"
18
19 #include "drmresources.h"
20 #include "platform.h"
21 #include "platformhisi.h"
22
23
24 #include <drm/drm_fourcc.h>
25 #include <cinttypes>
26 #include <stdatomic.h>
27 #include <xf86drm.h>
28 #include <xf86drmMode.h>
29
30 #include <cutils/log.h>
31 #include <hardware/gralloc.h>
32 #include "gralloc_priv.h"
33
34
35 namespace android {
36
37 Importer *Importer::CreateInstance(DrmResources *drm) {
38   HisiImporter *importer = new HisiImporter(drm);
39   if (!importer)
40     return NULL;
41
42   int ret = importer->Init();
43   if (ret) {
44     ALOGE("Failed to initialize the hisi importer %d", ret);
45     delete importer;
46     return NULL;
47   }
48   return importer;
49 }
50
51 HisiImporter::HisiImporter(DrmResources *drm) : DrmGenericImporter(drm), drm_(drm) {
52 }
53
54 HisiImporter::~HisiImporter() {
55 }
56
57 int HisiImporter::Init() {
58   int ret = hw_get_module(GRALLOC_HARDWARE_MODULE_ID,
59                           (const hw_module_t **)&gralloc_);
60   if (ret) {
61     ALOGE("Failed to open gralloc module %d", ret);
62     return ret;
63   }
64
65   if (strcasecmp(gralloc_->common.author, "ARM Ltd."))
66     ALOGW("Using non-ARM gralloc module: %s/%s\n", gralloc_->common.name,
67           gralloc_->common.author);
68
69   return 0;
70 }
71
72 EGLImageKHR HisiImporter::ImportImage(EGLDisplay egl_display, buffer_handle_t handle) {
73   private_handle_t const *hnd = reinterpret_cast < private_handle_t const *>(handle);
74   if (!hnd)
75     return NULL;
76
77   EGLint fmt = ConvertHalFormatToDrm(hnd->req_format);
78   if (fmt < 0)
79         return NULL;
80
81   EGLint attr[] = {
82     EGL_WIDTH, hnd->width,
83     EGL_HEIGHT, hnd->height,
84     EGL_LINUX_DRM_FOURCC_EXT, fmt,
85     EGL_DMA_BUF_PLANE0_FD_EXT, hnd->share_fd,
86     EGL_DMA_BUF_PLANE0_OFFSET_EXT, 0,
87     EGL_DMA_BUF_PLANE0_PITCH_EXT, hnd->byte_stride,
88     EGL_NONE,
89   };
90   return eglCreateImageKHR(egl_display, EGL_NO_CONTEXT, EGL_LINUX_DMA_BUF_EXT, NULL, attr);
91 }
92
93 int HisiImporter::ImportBuffer(buffer_handle_t handle, hwc_drm_bo_t *bo) {
94   private_handle_t const *hnd = reinterpret_cast < private_handle_t const *>(handle);
95   if (!hnd)
96     return -EINVAL;
97
98   uint32_t gem_handle;
99   int ret = drmPrimeFDToHandle(drm_->fd(), hnd->share_fd, &gem_handle);
100   if (ret) {
101     ALOGE("failed to import prime fd %d ret=%d", hnd->share_fd, ret);
102     return ret;
103   }
104
105   EGLint fmt = ConvertHalFormatToDrm(hnd->req_format);
106   if (fmt < 0)
107         return fmt;
108
109   memset(bo, 0, sizeof(hwc_drm_bo_t));
110   bo->width = hnd->width;
111   bo->height = hnd->height;
112   bo->format = fmt;
113   bo->usage = hnd->usage;
114   bo->pitches[0] = hnd->byte_stride;
115   bo->gem_handles[0] = gem_handle;
116   bo->offsets[0] = 0;
117
118   ret = drmModeAddFB2(drm_->fd(), bo->width, bo->height, bo->format,
119                       bo->gem_handles, bo->pitches, bo->offsets, &bo->fb_id, 0);
120   if (ret) {
121     ALOGE("could not create drm fb %d", ret);
122     return ret;
123   }
124
125   return ret;
126 }
127
128 std::unique_ptr<Planner> Planner::CreateInstance(DrmResources *) {
129   std::unique_ptr<Planner> planner(new Planner);
130   planner->AddStage<PlanStageGreedy>();
131   return planner;
132 }
133 }
134
135