OSDN Git Service

Ensure we close all the gem handles.
[android-x86/external-IA-Hardware-Composer.git] / os / android / grallocbufferhandler.cpp
1 /*
2  * Copyright (C) 2016 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 #include "grallocbufferhandler.h"
18
19 #include <xf86drmMode.h>
20 #include <xf86drm.h>
21
22 #include <hardware/hardware.h>
23 #include <hardware/hwcomposer.h>
24 #include <ui/GraphicBuffer.h>
25 #include <cutils/native_handle.h>
26 #include "commondrmutils.h"
27
28 #ifdef USE_MINIGBM
29 #include <cros_gralloc_handle.h>
30 #include <cros_gralloc_helpers.h>
31 #else
32 #include <gralloc_drm_handle.h>
33 #endif
34
35 #include <hwcdefs.h>
36 #include <hwctrace.h>
37 #include "drmhwcgralloc.h"
38 #include "commondrmutils.h"
39 #include "utils_android.h"
40
41 namespace hwcomposer {
42
43 // static
44 NativeBufferHandler *NativeBufferHandler::CreateInstance(uint32_t fd) {
45   GrallocBufferHandler *handler = new GrallocBufferHandler(fd);
46   if (!handler)
47     return NULL;
48
49   if (!handler->Init()) {
50     ETRACE("Failed to initialize GralocBufferHandlers.");
51     delete handler;
52     return NULL;
53   }
54   return handler;
55 }
56
57 GrallocBufferHandler::GrallocBufferHandler(uint32_t fd) : fd_(fd) {
58 }
59
60 GrallocBufferHandler::~GrallocBufferHandler() {
61 }
62
63 bool GrallocBufferHandler::Init() {
64   int ret = hw_get_module(GRALLOC_HARDWARE_MODULE_ID,
65                           (const hw_module_t **)&gralloc_);
66   if (ret) {
67     ETRACE("Failed to open gralloc module");
68     return false;
69   }
70   return true;
71 }
72
73 bool GrallocBufferHandler::CreateBuffer(uint32_t w, uint32_t h, int format,
74                                         HWCNativeHandle *handle) {
75   return CreateGraphicsBuffer(w, h, format, handle);
76 }
77
78 bool GrallocBufferHandler::ReleaseBuffer(HWCNativeHandle handle) {
79   return ReleaseGraphicsBuffer(handle, fd_);
80 }
81
82 void GrallocBufferHandler::DestroyHandle(HWCNativeHandle handle) {
83   DestroyBufferHandle(handle);
84 }
85
86 #ifdef USE_MINIGBM
87 bool GrallocBufferHandler::ImportBuffer(HWCNativeHandle handle, HwcBuffer *bo) {
88   if (!handle->imported_handle_) {
89     ETRACE("could not find gralloc drm handle");
90     return false;
91   }
92
93   return ImportGraphicsBuffer(handle, bo, fd_);
94 }
95
96 uint32_t GrallocBufferHandler::GetTotalPlanes(HWCNativeHandle handle) {
97   auto gr_handle = (struct cros_gralloc_handle *)handle->imported_handle_;
98   if (!gr_handle) {
99     ETRACE("could not find gralloc drm handle");
100     return false;
101   }
102
103   return drm_bo_get_num_planes(gr_handle->format);
104 }
105 #else
106 bool GrallocBufferHandler::ImportBuffer(HWCNativeHandle handle, HwcBuffer *bo) {
107   hwc_drm_bo_t hwc_bo;
108   int ret = gralloc_->perform(gralloc_, GRALLOC_MODULE_PERFORM_DRM_IMPORT, fd_,
109                               handle->handle_, &hwc_bo);
110   if (ret) {
111     ETRACE("GRALLOC_MODULE_PERFORM_DRM_IMPORT failed %d", ret);
112     return false;
113   }
114
115   memset(bo, 0, sizeof(struct HwcBuffer));
116   gralloc_->registerBuffer(gralloc_, handle->handle_);
117   gralloc_drm_handle_t *gr_handle = gralloc_drm_handle(handle->handle_);
118   bo->width = hwc_bo.width;
119   bo->height = hwc_bo.height;
120   bo->format = hwc_bo.format;
121   for (uint32_t i = 0; i < 4; i++) {
122     bo->pitches[i] = hwc_bo.pitches[i];
123     bo->offsets[i] = hwc_bo.offsets[i];
124     bo->gem_handles[i] = hwc_bo.gem_handles[i];
125   }
126   if (!gr_handle) {
127     ETRACE("could not find gralloc drm handle");
128     return false;
129   }
130
131   if (gr_handle->usage & GRALLOC_USAGE_PROTECTED) {
132     bo->usage |= hwcomposer::kLayerProtected;
133   } else if (gr_handle->usage & GRALLOC_USAGE_CURSOR) {
134     bo->usage |= hwcomposer::kLayerCursor;
135   }
136
137   bo->prime_fd = gr_handle->prime_fd;
138
139   return true;
140 }
141
142 // stubs
143 uint32_t GrallocBufferHandler::GetTotalPlanes(HWCNativeHandle /*handle*/) {
144   return 0;
145 }
146 #endif
147
148 void GrallocBufferHandler::CopyHandle(HWCNativeHandle source,
149                                       HWCNativeHandle *target) {
150   CopyBufferHandle(source, target);
151 }
152
153 void *GrallocBufferHandler::Map(HWCNativeHandle /*handle*/, uint32_t /*x*/,
154                                 uint32_t /*y*/, uint32_t /*width*/,
155                                 uint32_t /*height*/, uint32_t * /*stride*/,
156                                 void ** /*map_data*/, size_t /*plane*/) {
157   return NULL;
158 }
159
160 void GrallocBufferHandler::UnMap(HWCNativeHandle /*handle*/,
161                                  void * /*map_data*/) {
162 }
163
164 }  // namespace hwcomposer